Python dict difference between items and iteritems

Introduction

Welcome to a new code snippet in Python. Today, we are going to clarify the difference between items() and iteritems() methods when using a Python dictionary. Let us first quickly refresh our memory about lists, tuples and dictionaries in Python:

  • A list is an array data structure that can be edited
  • A tuple is an immutable (i.e. cannot be edited) array
  • A dictionary (i.e. hash table) is a list of key value pairs. It is a fast data structure (O(1) running time)

Here are few examples:

The output will be

Let us now proceed with the comparison. You have probably encountered Python code using items(), iteritems() and viewitems(). This is confusing for sure, so what is the difference between all these methods? The root cause of all that confusion is not realizing which Python version we are referring to. It is a historical issue that started in Python 2.x and got improved in Python 3.x.

Let us see how…

Python 2.x

Python 2.x has the dictionary method items() which returns a list of tuples. Here is how a list of tuples looks like:

The output will be

If the dictionary is large in size, the returned list requires more memory space which is not an efficient implementation. A better way to do it is to use iterators. Iterators is beyond the scope of this article. For more information about that, take a look at the following post. Using iterators, a new method was introduced that is iteritems() and the old method items() was kept for backward compatibility. So iteritems() is better however we cannot use it while adding or deleting entries from the dictionary. For that reason, Python 2.6 introduced a new method called viewitems() which returns a dynamic view of the dictionary key value pairs. It allows us to traverse the dictionary and edit at the same time. As you can see, we ended up with three methods.

That is confusing! Python 3 makes our life easy, let us see that…

Python 3.x

In Python 3, there is only one method named items(). It uses iterators so it is fast and allows traversing the dictionary while editing. Note that the method iteritems() was removed from Python 3. Let us take some examples:

Python 2.x examples

  • Example 1

This will print all tuples in the dictionary as a list

  • Example 2

iteritems() returns an iterator object so you need to call the next method to get individual tuples

  • Example 3

Since I is an iterator object, we can use a for loop against it to print individual tuples

  • Example 4

You cannot change the dictionary while iterating (recall that iteritems() returns an iterator object). The solution is to use viewitems()

  • Example 5

viewitems() returns a view to the dictionary items so it will work if you change the dictionary

Python 3.x

  • Example 1

The returned I object is a view class to the dictionary items

  • Example 2

This will generate an error

  • Example 3

This will generate an error:

Summary

Python 2.xPython 3.x
items() dictionary method returns a list of tuplesitems() method does not return a list of tuples
iteritems() dictionary method returns an iterator not the full list
iteritems() method is removed from Python 3.x
The dictionary cannot be edited while using iteritems()
The dictionary can be edited while using items()
viewitems() returns a dynamic view of the dictionary key value pairs. It enables traversing the dictionary while editing its content
items() returns a dynamic view of the dictionary key value pairs. It enables traversing the dictionary while editing its content

Thanks for visiting. Please use the comments section below for feedback

Tags:

Add a Comment

Your email address will not be published. Required fields are marked *