Syntax Cache
BlogMethodFeaturesHow It WorksBuild a Game
  1. Home
  2. Python
  3. Python Collections Practice: Lists, Dicts, Sets & Tuples
Python86 exercises

Python Collections Practice: Lists, Dicts, Sets & Tuples

Practice the Python collection ops you always mix up: append vs extend, dict.get defaults, set membership, tuple unpacking, and common interview patterns.

Common ErrorsQuick ReferencePractice
Warm-up1 / 2

Can you write this from memory?

Create an empty list called items

On this page
  1. 1Quick decision: which collection?
  2. 2Performance at a glance
  3. 3append vs extend
  4. 4dict.get() vs d[key]: when to use which
  5. 5sort() vs sorted(): in-place or new list?
  6. 6Common recipes
  7. Word frequency with Counter
  8. Group items by key
  9. Dedupe while preserving order
  10. Merge dicts (Python 3.9+)
  11. Safe delete from dict
  12. 7Pitfalls gallery
  13. 1. append vs extend confusion
  14. 2. Modifying list while iterating
  15. 3. Mutable dict key
  16. 4. The single-element tuple trap
  17. 8References
Quick decision: which collection?Performance at a glanceappend vs extenddict.get() vs d[key]: when to use whichsort() vs sorted(): in-place or new list?Common recipesPitfalls galleryReferences

If you hesitate on Python's "append vs extend" or whether to use dict.get() vs d[key], this page is for you.

Here you'll find a quick "which collection should I use?" guide, the core methods, common pitfalls (with fixes), and interview-style examples like frequency counting.

Use it as a refresher before coding sessions or interviews.

Related Python Topics
Python Comprehensions: List, Dict, Set & Generator ExpressionsPython Loops: range(), enumerate(), zip(), break/continue, for-elsePython Function Arguments: defaults, *args, keyword-only, **kwargs

Lists for ordered sequences, dicts for key-value lookup, sets for uniqueness and O(1) membership, tuples for immutable records and hashable keys.

Which collection to use: decision tree from key-value lookup to dict, uniqueness to set, mutability to list vs tuple

NeedUseWhy
Ordered sequence, may changelistAppend, pop, slice—general workhorse
Key → value lookupdictO(1) access by key
Uniqueness / fast "is X in here?"setO(1) membership, auto-dedupe
Fixed record / hashable keytupleImmutable, so usable as dict key

For a quick copy-paste reference of dict operations, see the Python dictionary methods cheat sheet.


Ready to practice?

Start practicing Python Collections: Lists, Dicts, Sets & Tuples with spaced repetition

x in collection is O(n) for lists but O(1) for sets and dicts. If you're doing frequent membership checks, switch from list to set.

Operationlistdictset
Access by indexO(1)——
Access by key—O(1) avg—
Search (x in)O(n)O(1) avgO(1) avg
Append/addO(1)O(1) avgO(1) avg
Insert at indexO(n)——
Delete by valueO(n)O(1) avgO(1) avg

Key insight: If you're doing x in collection frequently, switch from list to set for O(1) lookups.


append(x) adds one item (even if it's a list—you get nesting). extend(xs) unpacks the iterable and adds each element separately.

Here's the rule:

  • append(x) adds one item to the list—even if that item is itself a list.
  • extend(xs) unpacks the iterable and adds each element.
nums = [1, 2]
nums.append([3, 4])   # → [1, 2, [3, 4]]  (nested!)
nums = [1, 2]
nums.extend([3, 4])   # → [1, 2, 3, 4]    (flat)

Memory trick: "append appends one thing, extend extends by many."


# Raises KeyError if missing:
value = config["timeout"]

# Returns default if missing:
value = config.get("timeout", 30)

Use d[key] when a missing key is a bug—you want the error. Use get(key, default) when missing keys are expected (optional config, sparse data).

For insert-if-missing patterns, use setdefault():

counts.setdefault(word, 0)
counts[word] += 1

Or use collections.defaultdict to avoid the boilerplate entirely. When you find yourself constructing complex nested data structures step by step, that's a sign the Builder design pattern may be a good fit.


# In-place sort (modifies original, returns None):
nums = [3, 1, 2]
nums.sort()  # nums is now [1, 2, 3]

# New sorted list (original unchanged):
nums = [3, 1, 2]
ordered = sorted(nums)  # ordered is [1, 2, 3], nums still [3, 1, 2]

Use sort() when you don't need the original order. Use sorted() when you need both versions, or when sorting non-lists (tuples, generators).

Both accept key= and reverse=:

# Sort by length, descending:
words.sort(key=len, reverse=True)

Word frequency with Counter

from collections import Counter

text = "the quick brown fox jumps over the lazy dog"
counts = Counter(text.split())
print(counts.most_common(3))  # [('the', 2), ('quick', 1), ('brown', 1)]

Group items by key

from collections import defaultdict

orders = [{"customer": "alice", "total": 50}, {"customer": "bob", "total": 30}, {"customer": "alice", "total": 20}]
by_customer = defaultdict(list)
for order in orders:
    by_customer[order["customer"]].append(order)
# {'alice': [{...}, {...}], 'bob': [{...}]}

Dedupe while preserving order

seen = set()
unique = []
for x in items:
    if x not in seen:
        seen.add(x)
        unique.append(x)

Or in Python 3.7+, use dict.fromkeys():

unique = list(dict.fromkeys(items))

For a more concise approach, comprehensions let you build filtered or transformed collections in a single expression.

Merge dicts (Python 3.9+)

merged = defaults | overrides  # overrides wins on conflict

For earlier versions: {**defaults, **overrides}

Safe delete from dict

# pop returns the value (or default), no KeyError:
removed = d.pop("key", None)

# del raises KeyError if missing:
if "key" in d:
    del d["key"]

1. append vs extend confusion

# Bug: wanted flat list, got nested
result = []
result.append(get_items())  # oops, nested list!
# Fix:
result.extend(get_items())

2. Modifying list while iterating

# Bug: skips items or crashes
for x in items:
    if should_remove(x):
        items.remove(x)

# Fix: iterate over a copy
for x in items[:]:
    if should_remove(x):
        items.remove(x)

# Or: use comprehension
items = [x for x in items if not should_remove(x)]

3. Mutable dict key

# Bug: TypeError: unhashable type 'list'
cache = {[1, 2]: "value"}

# Fix: use tuple
cache = {(1, 2): "value"}

4. The single-element tuple trap

not_a_tuple = (42)     # just 42
actually_tuple = (42,) # tuple with one element

  • Python Tutorial: Data Structures
  • collections.Counter docs
  • Time complexity (Python wiki)

When to Use Python Collections: Lists, Dicts, Sets & Tuples

  • Use **lists** for ordered sequences, stacks/queues, and "do this in order" workflows.
  • Use **dicts** for fast lookup by key, structured records, grouping, and counting.
  • Use **sets** for uniqueness, deduping, and fast membership checks (`x in s` is O(1)).
  • Use **tuples** for fixed "record-like" data, multi-return values, and hashable dict keys.

Check Your Understanding: Python Collections: Lists, Dicts, Sets & Tuples

Prompt

Count the frequency of words in a sentence.

What a strong answer looks like

Use a dict (or `collections.Counter`). Loop once (O(n)) and increment counts. Mention why dict lookups are O(1) average, and why Counter is concise with `most_common()`.

What You'll Practice: Python Collections: Lists, Dicts, Sets & Tuples

Choosing list vs dict vs set vs tupleList methods (append, extend, insert, pop, remove)Dict methods (get, setdefault, keys, values, items, update)Counting & grouping patterns (dict / Counter / defaultdict)Set operations (union |, intersection &, difference -)Tuple unpacking and multi-returnNested data structures (list of dicts, dict of lists)Safe deletion from dict (pop with default, del with check)Sorting (sort vs sorted, key functions, reverse)

Common Python Collections: Lists, Dicts, Sets & Tuples Pitfalls

  • Confusing `append` (one item) vs `extend` (many items)—visualize the difference
  • KeyError when accessing missing dict keys—use `get()` or `setdefault()`
  • Modifying a list while iterating over it—iterate over a copy or use comprehension
  • Using a mutable type (list, dict) as a dict key or set item—use tuple instead
  • Creating a "not-a-tuple" by forgetting the comma: `(x)` is just `x`, use `(x,)`

Python Collections: Lists, Dicts, Sets & Tuples FAQ

append() or extend()?

`append(x)` adds one item (even if x is a list—it becomes a nested element). `extend(xs)` adds every element from an iterable.

dict.get() or d[key]?

Use `d[key]` when missing keys are a bug (you want KeyError). Use `get(key, default)` when missing keys are expected and you have a sensible fallback.

List or tuple?

Use list for mutable sequences. Use tuple for fixed, immutable records or when you need a hashable value (e.g., dict key).

Why use set instead of list?

Set membership checks (`x in s`) are O(1) average—much faster than scanning a list for large inputs.

When should I use collections.Counter?

When you're counting things. It's a dict subclass with helpful methods like `most_common()` and arithmetic operators.

sort() or sorted()?

`list.sort()` sorts in-place and returns None. `sorted(iterable)` returns a new sorted list, leaving the original unchanged.

What are the built-in collection types in Python?

Python has four built-in collection types: lists (ordered, mutable), tuples (ordered, immutable), dicts (key-value mapping), and sets (unordered, unique items). The `collections` module adds specialized types like `defaultdict`, `Counter`, `deque`, and `OrderedDict`.

Python Collections: Lists, Dicts, Sets & Tuples Syntax Quick Reference

append vs extend
xs.append([1, 2])  # one nested list
xs.extend([1, 2])  # two flat items
Dict safe access
value = my_dict.get("key", "default")
Count frequency
from collections import Counter
counts = Counter(words)
counts.most_common(3)
Set membership
if item in my_set:  # O(1) lookup
    ...
Tuple unpacking
x, y = point
first, *rest = items
Set operations
common = a & b      # intersection
all_items = a | b   # union
Safe dict delete
removed = d.pop("key", None)  # no KeyError
Grouping pattern
from collections import defaultdict
groups = defaultdict(list)
for item in items:
    groups[item.category].append(item)
sort vs sorted
nums.sort()              # in-place, returns None
new_list = sorted(nums)  # returns new list

Python Collections: Lists, Dicts, Sets & Tuples Sample Exercises

Example 1Difficulty: 1/5

Create a list called items containing "", "default", "test"

items = ["", "default", "test"]
Example 2Difficulty: 1/5

Check if "" is in items

"" in items
Example 3Difficulty: 2/5

Extend items with items from new_items

items.extend(new_items)

+ 83 more exercises

Quick Reference
Python Collections: Lists, Dicts, Sets & Tuples Cheat Sheet →

Copy-ready syntax examples for quick lookup

Further Reading

  • GDScript Dictionary map() and map_in_place12 min read

Related Design Patterns

Builder Pattern

Also in Other Languages

JavaScript Collections Practice

Start practicing Python Collections: Lists, Dicts, Sets & Tuples

Free daily exercises with spaced repetition. No credit card required.

← Back to Python Syntax Practice
Syntax Cache

Build syntax muscle memory with spaced repetition.

Product

  • Pricing
  • Our Method
  • Daily Practice
  • Design Patterns
  • Interview Prep

Resources

  • Blog
  • Compare
  • Cheat Sheets
  • Vibe Coding
  • Muscle Memory

Languages

  • Python
  • JavaScript
  • TypeScript
  • Rust
  • SQL
  • GDScript

Legal

  • Terms
  • Privacy
  • Contact

© 2026 Syntax Cache

Cancel anytime in 2 clicks. Keep access until the end of your billing period.

No refunds for partial billing periods.