Skip to main content
  1. Notes/
  2. Python 🐍/

⚑ List and Dictionary Comprehension in Python | Lesson 11

Python Progmramming Course
Table of Contents
Python - This article is part of a series.
Part 11: This Article

⚑ List and Dictionary Comprehension in Python
#

List and dictionary comprehensions are powerful, compact ways to create and transform lists or dictionaries in a single line of code. They help you write cleaner, faster, and more readable code compared to traditional loops! Let’s explore how to use them with examples and a fun small project. πŸš€


1. List Comprehension πŸ“
#

A concise way to create or transform a list.

Syntax:
#

[expression for item in iterable if condition]
  • expression is what you want to include in the new list.
  • iterable is a sequence or collection you loop over.
  • Optional if condition filters items.

Example:
#

Create a list of squares for numbers 1 to 5.

squares = [x**2 for x in range(1, 6)]
print(squares)  # [1, 4, 9, 16, 25]

Filter to include only even squares:

even_squares = [x**2 for x in range(1, 6) if x % 2 == 0]
print(even_squares)  # [4, 16]

2. Dictionary Comprehension πŸ“š
#

Create a dictionary from an iterable, also compact and clear.

Syntax:
#

{key_expression: value_expression for item in iterable if condition}

Example:
#

Create a dictionary where keys are numbers 1 to 5 and values are their cubes.

cubes = {x: x**3 for x in range(1, 6)}
print(cubes)  
# {1: 1, 2: 8, 3: 27, 4: 64, 5: 125}

Filter to include only numbers where cube is > 20:

filtered_cubes = {x: x**3 for x in range(1, 6) if x**3 > 20}
print(filtered_cubes)  
# {3: 27, 4: 64, 5: 125}

Small Project: Create a Student Grade Dictionary πŸŽ“
#

Goal: Given a list of student names and their scores, create a dictionary that stores each student’s name as the key and their grade (Pass/Fail) as the value based on if the score is β‰₯ 50.

Step 1: Define the data
#

students = ["Riya", "Aman", "Nina", "Sam"]
scores = [65, 45, 75, 30]

Step 2: Use dictionary comprehension with condition
#

grade_dict = {students[i]: ("Pass" if scores[i] >= 50 else "Fail") for i in range(len(students))}
print(grade_dict)

Output:
#

{'Riya': 'Pass', 'Aman': 'Fail', 'Nina': 'Pass', 'Sam': 'Fail'}

Practice Questions πŸ†
#

  1. Use list comprehension to create a list of even numbers between 1 and 20.
  2. Use dictionary comprehension to create a dictionary of numbers (1 to 10) and their squares.
  3. Modify the student project to add a “Distinction” grade for scores β‰₯ 75.

Checklist for This Chapter βœ…
#

  • Used list comprehension for creating lists
  • Used dictionary comprehension to build dictionaries
  • Applied conditions inside comprehensions
  • Built a practical small project application

You’re now ready to write elegant, efficient code with comprehensions! Keep experimenting and watch your programs get cleaner and faster! ✨

Aryan
Author
Aryan
A little bit about you
Python - This article is part of a series.
Part 11: This Article

Related

Introduction to Python 🐍 | Lesson 01
Python Progmramming Course
⏰ Working with Date and Time in Python | Lesson 16
Python Progmramming Course
🎬 Strings in Python | Lesson 04
Python Progmramming Course
πŸ—οΈ Object-Oriented Programming (OOP) Concepts in Python | Lesson 15
Python Progmramming Course
πŸ‘Ά Python Basics | Lesson 02
Python Progmramming Course
πŸ“„ File Handling in Python | Lesson 14
Python Progmramming Course