Search code examples
pythondictionaryordereddictionary

can we access key and value in the ordereddict in python.?


I worked to access the item in ordered dictionary. d is the ordered dictionary:

print d.items()

Here the output is a pair. I want to access the key and value in this pair.


Solution

  • You can unpack the key, value (a tuple) as below:

    for key, value in d.items():
        print (key)
        print (value)
    

    This works both on python 2 and 3.

    From docs:

    Return a new view of the dictionary’s items ((key, value) pairs).