Monday, November 14, 2011

Data types

New in Python 3: Dictionary
In python 3, keys(),values() and items() methods don't return a list as in previous versions. They return an iterable object. i.e. IUPAC={'A': 'Ala', 'C': 'Cys','E': 'Glu'}
>IUPAC.values() # under python 3.0, you will get:
<dict_values object at 0xb7x8s7s2>
>for x in IUPAC.values():
        print x
Ala
Cys
Glu

This is more memory efficient than Python 2.x, since Python doesn't store the whole list in memory.
If you need to get the list anyway, use list()
>list(IUPAC.values())
['Ala', 'Cys', 'Glu']

No comments:

Post a Comment