Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Dictionary

Tags:

python

I'm doing dictionaries in python now, and im trying to access the profile.keys and profile.items commands. All that happens though is something like <dict_values object at 0x107fa50> shows up. What should i do?

like image 646
Billjk Avatar asked Feb 10 '12 02:02

Billjk


2 Answers

you have to call like this profile.keys() and profile.items(). They are methods.

If you omit the brackets, you don't actually call the function. Instead you get a reference to the function object.

>>> mydict = {'foo':'bar'}
>>> mydict.keys
<built-in method keys of dict object at 0x01982540>
>>> mydict.keys()
['foo']
>>>
like image 102
RanRag Avatar answered Nov 15 '22 20:11

RanRag


If you want to iterate over keys:

for x in profile.keys():
  print x

or values

for x in profile.values():
  print x

or key-value pairs:

for x, y in profile.items():
  print x, y

or just assign to a variable for later use

x = profile.items()

etc.

like image 20
Joe Avatar answered Nov 15 '22 22:11

Joe