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 Python
Below are some of the ways by which we can iterate list of dictionaries in Python:
- Using a simple loop
- Using a list comprehension
- Using itertools.chain() Method
Iterate List Of Dictionaries Using a Simple Loop
In this example, a list named movies
contains dictionaries representing movie information. Through nested loops, the code iterates over each dictionary and unpacks its key-value pairs, printing the details of each movie, including title, director, genre, and year.
Python3
movies = [ { "title" : "The Shawshank Redemption" , "director" : "Frank Darabont" , "genre" : "Drama" , "year" : 1994 }, { "title" : "The Godfather" , "director" : "Francis Ford Coppola" , "genre" : "Crime" , "year" : 1972 } ] # Iterating through the list of dictionaries and unpacking key-value pairs for movie in movies: for key, value in movie.items(): print (f "{key}: {value}" ) |
title: The Shawshank Redemption director: Frank Darabont genre: Drama year: 1994 title: The Godfather director: Francis Ford Coppola genre: Crime year: 1972
Iterate List Of Dictionaries Using a List Comprehension
In this example, a list named movies
contains dictionaries representing movie information. The code utilizes a list comprehension to iterate over each dictionary, unpacking its key-value pairs, and printing them in a concise manner using the print
function.
Python3
movies = [ { "title" : "The Shawshank Redemption" , "director" : "Frank Darabont" , "genre" : "Drama" , "year" : 1994 }, { "title" : "The Godfather" , "director" : "Francis Ford Coppola" , "genre" : "Crime" , "year" : 1972 } ] # Iterating through the list of dictionaries and unpacking key-value pairs _ = [ print (key, value) for d in movies for key, value in d.items()] |
title The Shawshank Redemption director Frank Darabont genre Drama year 1994 title The Godfather director Francis Ford Coppola genre Crime year 1972
Iterate List Of Dictionaries Using itertools.chain() Method
In this example, the itertools
module’s chain()
function is used to iterate through the list of dictionaries (movies
). The nested generator expression and chain.from_iterable
method unpack key-value pairs from each dictionary, and the resulting pairs are iterated through, printing the details of each movie, including title, director, genre, and year.
Python3
from itertools import chain movies = [ { "title" : "The Shawshank Redemption" , "director" : "Frank Darabont" , "genre" : "Drama" , "year" : 1994 }, { "title" : "The Godfather" , "director" : "Francis Ford Coppola" , "genre" : "Crime" , "year" : 1972 }, { "title" : "Inception" , "director" : "Christopher Nolan" , "genre" : "Sci-Fi" , "year" : 2010 }, { "title" : "Pulp Fiction" , "director" : "Quentin Tarantino" , "genre" : "Crime" , "year" : 1994 } ] # Iterating through the list of dictionaries and unpacking key-value pairs for key, value in chain.from_iterable(d.items() for d in movies): print (key, value) |
Output:
title The Shawshank Redemption
director Frank Darabont
genre Drama
year 1994
title The Godfather
director Francis Ford Coppola
genre Crime
year 1972
title Inception
director Christopher Nolan
genre Sci-Fi
year 2010
title Pulp Fiction
director Quentin Tarantino
genre Crime
year 1994
Conclusion
This article, explored the efficient iteration methods through a list of dictionaries in Python, covering key concepts such as Lists of Dictionaries, Dictionary Unpacking, and Iterating through Lists. The provided examples, including student information, book details, and movie records, demonstrated the practical application of these concepts.We’ve seen the practical application of these concepts with examples, showcasing how to efficiently handle structured data using this approach.