user3504701
user3504701

Reputation: 45

subscriptable error when trying to make a dictionary in to a list

In my code that I am working on, I got an error message when I was trying to make a dictionary in to a list, and split that list in to three letter rna codons. This is what I inputted:

for i in range (0, len(self.codon_dict), 3): #in range from the first object to the last         object of the string, in multiples of three
            codon = (list(self.codon_dict.items[i:i+3])) #codons in string are read from      the first object (i) to another object three bases down the string 
            print (codon)
            if codon in NucParams.codon_dict():
                self.codon_dict[codon] +=1

and the error I had recieved was this:

 codon = (list(self.codon_dict.items[i:i+3])) #codons in string are read from the first object (i) to another object three bases down the string
TypeError: 'builtin_function_or_method' object is not subscriptable

what do they mean when they say an object isn't subscriptable? Also, how can I manage to fix this error? Thanks.

Note: NucParams is my class, while codon_dict is the dictionary that lists the three letter codons that happen to code for amino acids.

Upvotes: 0

Views: 960

Answers (1)

Rory Yorke
Rory Yorke

Reputation: 2236

First of all you're trying to subscript the method (or function) items, not the result of the function; you're missing parentheses () after self.codon_dict.items.

Secondly, assuming you're used to Python 2 like me, you might be surprised that dict.items() now returns a 'view' on the dictionary items; see this SO question for more.

Here's some simple sample code showing how you could use dict.items() in Python 3.

import itertools

d={'foo':'bar',
   'baz':'quuz',
   'fluml':'sqoob'}

print(list(d.items())[0:2])
print(list(itertools.islice(d.items(),0,2)))

Running this gives

[('foo', 'bar'), ('baz', 'quuz')]
[('foo', 'bar'), ('baz', 'quuz')]

Upvotes: 1

Related Questions