Python: Containers vs Comprehensions

Last updated: 2026-09-22

We've covered lists, tuples, dictionaries, and sets individually. Now let's do a side-by-side comparison — what are the differences, and when should you use each? We'll also thoroughly master comprehensions, one of Python's most powerful features.


Related Built-in Functions:split() set() tuple dict list if

1. Four Containers Compared

Feature List (list) Tuple (tuple) Dictionary (dict) Set (set)
Syntax [] () {key: value} {} or set()
Ordered ✅ Yes ✅ Yes ✅ Insertion order (3.7+) ❌ Unordered
Mutable ✅ ❌ ✅ ✅
Duplicates ✅ Allowed ✅ Allowed Keys unique, values can repeat ❌ Unique elements
Access Index [0] Index [0] Key ["key"] No index support
Lookup speed O(n) slow O(n) slow O(1) very fast O(1) very fast
Typical use Ordered data, dynamic collections Fixed data, function returns Mappings, caches Dedup, set operations
💡 Key selection principles: 1) Need access by position → list/tuple; 2) Need access by name → dictionary; 3) Need dedup or math operations → set; 4) Data shouldn't change → tuple.

▶ Example: Container Selection Comparison (Difficulty ⭐⭐)

▶ Try it Yourself

Output:

TEXT 📖 Display only
# Executed successfully


2. Advanced List Comprehensions

Lesson 11 covered basic comprehensions. Let's dive deeper.

(1) Multiple Conditions

PYTHON
numbers = range(1, 31)

# Divisible by 3 AND divisible by 5
filtered = [n for n in numbers if n % 3 == 0 and n % 5 == 0]
print(filtered)             # [15, 30]

# Divisible by 3 OR divisible by 5
filtered = [n for n in numbers if n % 3 == 0 or n % 5 == 0]
print(filtered)             # [3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24, 25, 27, 30]

(2) Nested Loop Comprehensions

PYTHON
# Generate coordinate pairs — equivalent to double for loop
pairs = [(x, y) for x in range(3) for y in range(3)]
print(pairs)
# [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]

# Nested with condition — find pairs where sum is even
pairs = [(x, y) for x in range(3) for y in range(3) if (x + y) % 2 == 0]
print(pairs)
# [(0, 0), (0, 2), (1, 1), (2, 0), (2, 2)]

(3) if-else in Comprehensions

PYTHON
# if-else in comprehensions — note the different position
numbers = [1, 2, 3, 4, 5, 6]

# Only if (filtering) — placed after
evens = [n for n in numbers if n % 2 == 0]

# if-else (transformation) — placed before
result = ["even" if n % 2 == 0 else "odd" for n in numbers]
print(result)   # ['odd', 'even', 'odd', 'even', 'odd', 'even']

▶ Example: Data Transformation Pipeline (Difficulty ⭐⭐⭐)

▶ Try it Yourself

Output:

TEXT 📖 Display only
['Apple', 'Banana', 'Cherry', 'Date']


3. Dictionary Comprehensions

Dictionaries can also use comprehensions. The syntax is similar to lists, but uses {} and specifies key-value pairs:

PYTHON
# Basic dict comprehension
squares = {x: x ** 2 for x in range(5)}
print(squares)              # {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}

# With condition
even_squares = {x: x ** 2 for x in range(10) if x % 2 == 0}
print(even_squares)         # {0: 0, 2: 4, 4: 16, 6: 36, 8: 64}

# Reverse keys and values
original = {"a": 1, "b": 2, "c": 3}
reversed_dict = {value: key for key, value in original.items()}
print(reversed_dict)        # {1: 'a', 2: 'b', 3: 'c'}

(1) Practical: Merge Two Lists into a Dict

PYTHON
# Merge two lists into a dictionary
names = ["Alice", "Bob", "Charlie"]
scores = [85, 92, 78]
result = {names[i]: scores[i] for i in range(len(names))}
print(result)               # {'Alice': 85, 'Bob': 92, 'Charlie': 78}

# Using zip() is more elegant
result = {name: score for name, score in zip(names, scores)}
print(result)               # {'Alice': 85, 'Bob': 92, 'Charlie': 78}


4. Set Comprehensions

Set comprehensions use {} with just an expression (no colon) — the difference from dict comprehensions is the presence or absence of a colon:

PYTHON
# Set comprehension — auto-deduplication
numbers = [1, 2, 2, 3, 3, 3, 4, 5, 5]
unique_squares = {x ** 2 for x in numbers}
print(unique_squares)       # {1, 4, 9, 16, 25}

# With condition
even_set = {x for x in range(20) if x % 2 == 0}
print(even_set)             # {0, 2, 4, 6, 8, 10, 12, 14, 16, 18}
💡 The three distinctions: [x for x in ...] is a list — ordered, duplicates allowed. {x: y for x in ...} is a dict — has a colon. {x for x in ...} is a set — no colon, unordered, deduplicated.

▶ Example: Word Analysis (Difficulty ⭐⭐⭐)

▶ Try it Yourself

Output:

TEXT 📖 Display only
Total words: 18
Unique words: 10
All unique words: ['also', 'but', 'favorite', 'great', 'is', 'javascript', 'love', 'my', 'python', 'i']


5. Common Use Cases


Related Built-in Functions:split() set() tuple dict list if

❓ FAQ

Q What's the difference between list comprehensions and generator expressions?
A List comprehensions [x for x in range(10)] generate all data at once in memory. Generator expressions (x for x in range(10)) are lazy — one value at a time, saving memory. Use generators for large datasets (tens of thousands+).
Q What if a comprehension has more than two levels of nesting?
A Comprehensions with more than two levels become unreadable. Code like [x for xs in matrix for ys in xs for x in ys] is nearly impossible to understand at a glance. If you need three+ levels, use regular for loops or helper functions.
Q How do I decide which container to use?
A Three questions: ① Do you need unique keys? → dictionary. ② Do you need dedup or set operations? → set. ③ Does data need to change? → list if yes, tuple if no. In most scenarios, lists and dictionaries cover 90% of use cases.

📖 Summary


📝 Exercises

  1. Basic (Difficulty ⭐): Use a list comprehension to generate all numbers between 10 and 50 that are divisible by 7 or contain the digit 7. Hint: Use or to combine two conditions.

  2. Intermediate (Difficulty ⭐⭐): Given two lists keys = ["name", "age", "city"] and values = ["Alice", 25, "Beijing"], use a dict comprehension to merge them into a dict. Hint: Use zip(keys, values) to pair them.

  3. Challenge (Difficulty ⭐⭐⭐): Write a "data analyzer." Given a string containing multiple sentences, count and output: total word count, unique word count, and the 3 most frequent words. Hint: Use split() for tokenization, a dictionary for counting, and sorted(dict.items(), key=lambda x: x[1], reverse=True)[:3] for the top 3.

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏