Difference between dict.items() and dict.iteritems() in Python
dict.items() and dict.iteriteams() almost 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
Python
d = {
"fantasy": "harrypotter",
"romance": "me before you",
"fiction": "divergent"
}
print d.iteritems()
|
Output:
<dictionary-itemiterator object at 0x7f04628d5890>
To print the dictionary items, use a for() loop to divide the objects and print them
Example-2
Python
d = {
"fantasy": "harrypotter",
"romance": "me before you",
"fiction": "divergent"
}
for i in d.iteritems():
print (i)
|
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
Python
d = {
"fantasy": "harrypotter",
"romance": "me before you",
"fiction": "divergent"
}
print (d.items())
print (d.iteritems())
|
Output:
[(‘romance’, ‘me before you’), (‘fantasy’, ‘harrypotter’), (‘fiction’, ‘divergent’)] <dictionary-itemiterator object at 0x7f1d78214890>
For Python3:
Example-1
Python3
d = {
"fantasy": "harrypotter",
"romance": "me before you",
"fiction": "divergent"
}
print (d.items())
|
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
Python3
d = {
"fantasy": "harrypotter",
"romance": "me before you",
"fiction": "divergent"
}
print ("d.items() in (v3. 6.2 ) = ")
for i in d.items():
print (i)
print ("\nd.iteritems() in (v3. 6.2 ) = ")
for i in d.iteritems():
print (i)
|
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'
Let us see the differences in a tabular form:
|
dict.items() |
dict.iteritems() |
1. |
The dict.items() method returns a view object. |
The dict.iteritems() function returns an iterator of the dictionary’s list. |
2. |
Its syntax is-:
dictionary.items()
|
The dict.iteritems() is a generator that yields 2-tuples |
3. |
It does not take any parameters. |
It does not take any parameters. |
4. |
Its return value is a list of tuple pairs. |
Its return value is the iterator on list of key value pairs. |
5. |
If the input list is empty then it returns an empty list. |
It is the feature of python2 version but gets removed in python3 version. |
Last Updated :
26 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...