1

How can I print a dictionary of queues without popping off the contents? I have something like this:

>>> mac = '\x04\xab\x4d'
>>> mydict = {}
>>> if mac in mydict.keys():
...     mydict[mac].append("test")
... else:
...     mydict[mac]=[]
...     mydict[mac].append("test")
... 
>>>
>>> for key,val in mydict.Items():
...     print key
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'Items'
>>> for key,val in mydict.Item():
...     print key

and am wondering how I can display the contents of my dictionary...

Thank you! Ron

stdcerr
  • 13,725
  • 25
  • 71
  • 128

1 Answers1

1

As @Rohit Jain correctly pointed out: there is no Items() method - there is items().

Though (if you are using python 2), it's generally better to use generator approach - use iteritems(), iterkeys(), itervalues():

>>> for key,val in mydict.iteritems():
...     print key, val
... 
�M ['test']
>>> for key in mydict.iterkeys():
...     print key
... 
�M
>>> for value in mydict.itervalues():
...     print value
... 
['test']

Note that in python 3, items(), keys() and values() return iterators.

Also see:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195