List, Set, and Dictionary Comprehensions
📘 Python
👁 48 views
📅 Nov 05, 2025
⏱ Estimated reading time: 2 min
List, Set, and Dictionary Comprehensions in Python
Python provides comprehensions as a concise way to create lists, sets, and dictionaries in a single line. They make code more readable and efficient.
1. List Comprehensions
-
Create a list by applying an expression to each item in an iterable.
-
Optional condition to filter items.
Syntax:
Examples:
2. Set Comprehensions
-
Similar to list comprehensions but create sets.
-
Removes duplicates automatically.
Syntax:
Examples:
3. Dictionary Comprehensions
-
Create dictionaries using key-value pairs in a concise format.
Syntax:
Examples:
4. Key Points
-
Comprehensions provide compact syntax for constructing sequences.
-
Can include conditions (
if) and nested loops. -
List comprehension → list
-
Set comprehension → set
-
Dictionary comprehension → dictionary
-
Improves readability and efficiency over traditional loops.
🔒 Some advanced sections are available for Registered Members
Register Now
Register Now
Share this Post
← Back to Tutorials