Atticus29
Atticus29

Reputation: 4412

object of type 'NoneType' has no len(), but I'm pretty sure my object is a valid list

The following module keeps failing for me, telling me that an object of type 'NoneType' has no len(), but it seems that the object being passed is a list, not an object of type 'NoneType'. I include the module and the output below.

def Purge_Polyploid_MisScores(dictOfLists):
  #print "dict getting passed to Purge_Polyploid_MisScores function", dictOfLists
  for x in dictOfLists.keys():
    for y in range (0, len(dictOfLists[x])):
      print "x", x, " and y", y
      print dictOfLists[x][y]
      #if not dictOfLists[x][y]:
        #print "error at ",x,dictOfLists[str(int(x)-1)][0]
      if len(dictOfLists[x][y])>3:
        try:
          dictOfLists[x][y]=dictOfLists[x][y].remove('**')
        except:
          for z in dictOfLists[x][y]:
            if dictOfLists[x][y].count(z)>2:
              print "removed ",z," at dictOfLists[",x,"][",y,"]", dictOfLists[x][y]
              dictOfLists[x][y].remove(z)
              #I think this produces an error: dictOfLists[x][y]=dictOfLists[x][y].remove(z)
              print "now, it looks like", dictOfLists[x][y]
        if len(dictOfLists[x][y])>3:
          print "The Length is still greater than 3! at dictOfLists[",x,"][",y,"]", dictOfLists[x][y]

          #print "the reason you have a polyploid is not a mis-score"
          #print "dictOfLists[",x,"][",y,"]",dictOfLists[x][y]
      print "Reached the end of the loop"
  return dictOfLists

Error/output preceding the error:

x 449  and y 100
['Yellow submarine', '273', '273']
Reached the end of the loop
x 449  and y 101
['Heartland', '250', '250', '250']
removed  250  at dictOfLists[ 449 ][ 101 ] ['Heartland', '250', '250', '250']
now, it looks like ['Heartland', '250', '250']
Reached the end of the loop
x 449  and y 102
['Julia', '116', '119', '**']
Traceback (most recent call last):
  File "fast_run.py", line 11, in <module>
    sample_names_list_e1_keys_as_numbers_e2=transpose.combine_allele_report_pipeline_dict(pipeline_directory, keeplist_address, rejected_samples_address)
  File "/Users/markfisher/transpose.py", line 887, in combine_allele_report_pipeline_dict
    samples=Purge_Polyploid_MisScores(samples)
  File "/Users/markfisher/transpose.py", line 1332, in Purge_Polyploid_MisScores
    if len(dictOfLists[x][y])>3:
TypeError: object of type 'NoneType' has no len() 

In other words, ['Julia', '116', '119', '**'] seems to be failing at if len(['Julia', '116', '119', '**'])>3, and I have no idea why.

I hope that I've equipped you guys with enough to see my error! Thanks!

Upvotes: 1

Views: 2838

Answers (2)

Phil Cooper
Phil Cooper

Reputation: 5877

@BrenBarn got the right answer and I know that this should be a comment, not an answer; but I can't post code in a comment well.

If in your loops you have dictOfLists[x][y] like nine times, something is wrong structurally.

  • use items() to get keys and values rather than just keys and then looking up the value
  • use enumerate to get the index and value in a list rather than iterating over range(len(

Something more like:

def Purge_Polyploid_MisScores(dictOfLists):
    for key,lst in dictOfLists.items():
        for i,val in enumerate(lst):
                print "key: %s index: %i val: %s"%(key,i,val)
                if len(val)>3:
                    val.remove('**')

sorry if the re-write offends but you put thought into posting test code (+1) so I wanted to get you constructive (hopefully) feeback in return

Upvotes: 1

BrenBarn
BrenBarn

Reputation: 251383

The problem is this: dictOfLists[x][y]=dictOfLists[x][y].remove('**'). The remove method of lists removes the element in-place, mutating the original list, and returns None, so you are setting the list to None. Instead, just do dictOfLists[x][y].remove('**').

Upvotes: 10

Related Questions