Difference between dict.items() and dict.iteritems() in Python

dict.items() and dict.iteriteams() almots does the same thing, but there is a slight difference between them –

  • dict.items(): returns a copy of the dictionary’s list in the form of (key, value) tuple pairs, which is a (Python v3.x) version, and exists in (Python v2.x) version.
  • dict.iteritems(): returns an iterator of the dictionary’s list in the form of (key, value) tuple pairs. which is a (Python v2.x) version and got omitted in (Python v3.x) version.

For Python2.x:

Example-1



filter_none

edit
close

play_arrow

link
brightness_4
code

# Python2 code to demonstrate
# d.iteritems()
  
  
d ={
  "fantasy": "harrypotter",
  "romance": "me before you",
  "fiction": "divergent"
  }
  
# every time you run the object address keeps changes
print d.iteritems()

chevron_right


Output:

<dictionary-itemiterator object at 0x7f04628d5890>

To print the dictionary items, use a for() loop to divide the objects and print them
Example-2

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python2 code to demonstrate
# d.iteritems()
  
d ={
"fantasy": "harrypotter",
"romance": "me before you",
"fiction": "divergent"
}
  
for i in d.iteritems():
      
    # prints the items
    print(i)

chevron_right


Output:

('romance', 'me before you')
('fantasy', 'harrypotter')
('fiction', 'divergent')

If we try to run the dict.items() in Python v2.x, it runs as dict.items() exists in v2.x.

Example-3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python2 code to demonstrate
# d.items()
  
  
d ={
  "fantasy": "harrypotter",
  "romance": "me before you",
  "fiction": "divergent"
  }
    
# places the tuples in a list.
print(d.items())
  
# returns iterators and never builds a list fully.
print(d.iteritems())

chevron_right


Output:

[(‘romance’, ‘me before you’), (‘fantasy’, ‘harrypotter’), (‘fiction’, ‘divergent’)]
<dictionary-itemiterator object at 0x7f1d78214890>

For Python3:

Example-1

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# d.items()
  
d ={
  "fantasy": "harrypotter",
  "romance": "me before you",
  "fiction": "divergent"
  }
  
# saves as a copy
print(d.items())
  

chevron_right


Output:

dict_items([(‘fantasy’, ‘harrypotter’), (‘fiction’, ‘divergent’), (‘romance’, ‘me before you’)])

If we try to run the dict.iteritems() in Python v3.x, we will ecounter with an error.

Example-2

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate
# d.iteritems()
  
  
d ={
  "fantasy": "harrypotter",
  "romance": "me before you",
  "fiction": "divergent"
  }
  
print("d.items() in (v3.6.2) = ")
for i in d.items():
  
    # prints the items
    print(i)
  
print("\nd.iteritems() in (v3.6.2)=")
  
for i in d.iteritems():
  
    # prints the items
    print(i)

chevron_right


Output:

d.items() in (v3.6.2) = 
('fiction', 'divergent')
('fantasy', 'harrypotter')
('romance', 'me before you')

d.iteritems() in (v3.6.2)=
Traceback (most recent call last):
  File "/home/33cecec06331126ebf113f154753a9a0.py", line 19, in 
    for i in d.iteritems():
AttributeError: 'dict' object has no attribute 'iteritems'


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.