Introduction

In Python, dictionaries are a powerful data structure that allow us to store and manipulate key-value pairs. Dictionary comprehension is a concise and elegant way of creating dictionaries in Python. With dictionary comprehension, we can create dictionaries with just one line of code, making it a very useful technique for data manipulation and processing. In this tutorial, we will explore the syntax and applications of dictionary comprehension in Python, and provide practical examples to help you grasp this concept.

Table of Contents :

  • Dictionary comprehension 
  • Usage of dictionary comprehensions
    • to transform a dictionary
    • to filter dictionary elements.

Dictionary comprehension :

  • Dictionary comprehension is a sophisticated and concise way of carrying on basic operations on dictionary elements.
  • Control flow of dictionary comprehensions is as follows :
    • The interpreter loops over all the elements of the dictionary.
    • performs some action (like filtering or modifying the value) on each element.
    • creates and returns a new dictionary.
  • The basic syntax for dictionary comprehensions is :  {key:value for (key,value) in d.items() if condition} 
  • Code Sample : 

quantities = {
    "apple": 10,
    "oranges": 20,
    "banana": 25,
    "Mangoes": 35
}

print("Getting fruits with quantity > 20")
fruits = {key:value for (key,value) in quantities.items() if value > 20}

print(f"Fruits with qnt > 20  = {fruits}")

# Output
# Getting fruits with quantity > 20
# Fruits with qnt > 20  = {'banana': 25, 'Mangoes': 35}


Usage of dictionary comprehensions :

  • We can use dictionary comprehensions for various purposes.
  • A couple of usages of dictionary comprehensions are : 
    • to transform a dictionary
    • to filter dictionary elements.

Transforming a dictionary using dictionary comprehension

  • We can use the  update()  method to transform a dictionary using dictionary comprehension.
  • Code Sample : 

quantities = {
    "apple": 10,
    "oranges": 20,
    "banana": 25,
    "Mangoes": 35
}

print(f"Quantities  = {quantities}")
print()

print("If quantity is more than 20 - set it to 15")
quantities.update({key:15 for key in quantities if quantities[key] > 20})
print(f"New Quantities  = {quantities}")


# Output
# Quantities  = {'apple': 10, 'oranges': 20, 'banana': 25, 'Mangoes': 35}

# If quantity is more than 20 - set it to 15
# New Quantities  = {'apple': 10, 'oranges': 20, 'banana': 15, 'Mangoes': 15}


Filtering dictionary elements using dictionary comprehension

  • We can filter out elements of dictionary using dictionary comprehension and conditional statements.
  • Code Sample : 

quantities = {
    "apple": 0,
    "oranges": 20,
    "banana": 0,
    "Mangoes": 35
}

print("Getting Out of stock fruits")
fruits = {key:value for (key,value) in quantities.items() if value == 0}

print(f"Out of stock fruits  = {fruits}")


# Output
# Getting Out of stock fruits
# Out of stock fruits  = {'apple': 0, 'banana': 0}


Prev. Tutorial : Looping through a dict

Next Tutorial : Sets in Python