Open In App

Why is iterating over a dictionary slow in Python?

Last Updated : 22 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to discuss why is iterating over a dict so slow in Python? Before coming to any conclusion lets have a look at the performance difference between NumPy arrays and dictionaries in python:

Python




# import modules
import numpy as np
import sys
  
# compute numpy performance
def np_performance():
    array = np.empty(100000000)
    for i in range(100000000):
        array[i] = i
    print("SIZE : "
          sys.getsizeof(array)/1024.0**2
          "MiB")
  
# compute dictionary performance
def dict_performance():
    dic = dict()
    for i in range(100000000):
        dic[i] = i
    print("SIZE : "
          sys.getsizeof(dic)/1024.0**2
          "MiB")


In the above Python Script we have two functions :

  1. np_performance: This function creates an empty NumPy array for 10,00,000 elements and iterates over the entire array updating individual element’s value to the iterator location (‘i’ in this case)
  2. dict_performance: This function creates an empty Dictionary for 10,00,000 elements and iterates over the entire dictionary updating individual element’s value to the iterator location (‘i’ in this case)

And finally, a sys.getsizeof() function call to calculate the memory usage by respective data structures.

Now we call both these functions, and to measure the time taken by each function we use %time function which gives us the Time execution of a Python statement or expression. %time function can be used both as a line and cell magic:

  1. In the inline mode, you can time a single-line statement (though multiple ones can be chained using semicolons).
  2. In cell mode, you can time the cell body (a directly following statement raises an error).

Calling these functions from inline mode using %time method :

Python3




# compute time taken
%time np_performance()


Output:

Python3




# compute time taken
%time dict_performance()


Output:

As we can see there is quite a difference in Wall time between iterating on a NumPy array and a python dictionary.

  • This difference in performance is due to the internal working differences between arrays and dictionaries as after Python 3.6, Dictionaries in python are based on a hybrid of HashTables and an array of elements. So whenever we take out/delete an entry from the dictionary, rather than deleting a key from that particular location, it allows the next key to be replaced by deleted key’s position. What python dictionary does is replace the value from the hash array with a dummy value representing null. So upon traversing when you encounter these dummy null values it keeps on iterating till it finds the next real-valued key.
  • Since there may be lots of empty spaces we’ll be traversing on without any real benefits and hence Dictionaries are generally slower than their array/list counterparts.
  • For large-sized Datasets, memory access would be the bottleneck. Dictionaries are mutable, and take up more memory than arrays or (named) tuples (when organized efficiently, not duplicating type information).


Previous Article
Next Article

Similar Reads

Iterating over rows and columns in Pandas DataFrame
Iteration is a general term for taking each item of something, one after another. Pandas DataFrame consists of rows and columns so, to iterate over dataframe, we have to iterate a dataframe like a dictionary. In a dictionary, we iterate over the keys of the object in the same way we have to iterate in dataframe. In this article, we are using "nba.c
6 min read
Python | Delete items from dictionary while iterating
A dictionary in Python is an ordered collection of data values. Unlike other Data Types that hold only a single value as an element, a dictionary holds the key: value pairs. Dictionary keys must be unique and must be of an immutable data type such as a: string, integer or tuple. Note: In Python 2 dictionary keys were unordered. As of Python 3, they
3 min read
What makes Python a slow language ?
Python is a high-level language (than C or C++) thus Python itself manages details of a program like memory allocation, memory deallocation, pointers, etc. This makes writing codes in Python easier for programmers. Python code is first compiled into python Byte Code. The Byte Code interpreter conversion happens internally and most of it is hidden f
2 min read
Creating a Slow Motion Video Using OpenCV - Python
In this article, we will try to create a slow-motion video using OpenCV( Open Source Computer Vision) library in Python. OpenCV ( is an open-source computer vision and machine learning software library. Now a video is basically a set of moving pictures moving at a rate of about 24 frames per second. If this frame rate is decreased then we get a slo
4 min read
Python: Iterating With Python Lambda
In Python, the lambda function is an anonymous function. This one expression is evaluated and returned. Thus, We can use lambda functions as a function object. In this article, we will learn how to iterate with lambda in python. Syntax: lambda variable : expression Where, variable is used in the expressionexpression can be an mathematical expressio
2 min read
Python | Iterating two lists at once
Sometimes, while working with Python list, we can have a problem in which we have to iterate over two list elements. Iterating one after another is an option, but it's more cumbersome and a one-two liner is always recommended over that. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + "+" operator The combina
5 min read
Python - Iterating through a range of dates
In this article, we will discuss how to iterate DateTime through a range of dates. Using loop and timedelta to Iterate through a range of dates Timedelta is used to get the dates and loop is to iterate the date from the start date to end date Syntax: delta = datetime.timedelta(days=1) Example: Python code to display the dates from 2021 - Feb 1st to
2 min read
Iterating List of Python Dictionaries
Iteration of the list of dictionaries is a very common practice that every developer performs while encountering with dictionary data. In this article, we will explore how to iterate through a list of dictionaries. Iterating List Of Dictionaries in PythonBelow are some of the ways by which we can iterate list of dictionaries in Python: Using a simp
3 min read
MoviePy – Iterating frames of Video File Clip
In this article we will see how we can iterate frames of the video file clip in MoviePy. MoviePy is a Python module for video editing, which can be used for basic operations on videos and GIF’s. A video is basically combination of lots of frame for each time there is a specific frame, in order to get the frame at given time we use get_frame method.
2 min read
Python - How to Iterate over nested dictionary ?
In this article, we will discuss how to iterate over a nested dictionary in Python. Nested dictionary means dictionary inside a dictionary and we are going to see every possible way of iterating over such a data structure. Nested dictionary in use: {'Student 1': {'Name': 'Bobby', 'Id': 1, 'Age': 20}, 'Student 2': {'Name': 'ojaswi', 'Id': 2, 'Age':
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg