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.
<dictionary-itemiterator object at 0x7f04628d5890>
To print the dictionary items, use a
for() loop to divide the objects and print them
('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.
[(‘romance’, ‘me before you’), (‘fantasy’, ‘harrypotter’), (‘fiction’, ‘divergent’)]
<dictionary-itemiterator object at 0x7f1d78214890>
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.
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'
- Difference between 'and' and '&' in Python
- Python set operations (union, intersection, difference and symmetric difference)
- Difference between == and is operator in Python
- Difference between Method and Function in Python
- Python | Difference between iterable and iterator
- Difference between List and Array in Python
- Python | Difference between Pandas.copy() and copying through variables
- Difference between List comprehension and Lambda in Python
- Difference between Python and C#
- Difference between C and Python
- Python | Difference Between List and Tuple
- Difference Between Go and Python Programming Language
- Difference between continue and pass statements in Python
- Difference between input() and raw_input() functions in Python
- Python - Difference between sorted() and sort()
- Difference between List and Dictionary in Python
- Python: Difference between Lock and Rlock objects
- Python program to find difference between current time and given time
- Python - Difference between json.dump() and json.dumps()
- Difference Between Python and Bash
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.