0% complete

Control flow and Python collections · Lesson 3 of 3

Lists, tuples, sets, and dictionaries

Select collections based on ordering, mutability, uniqueness, and lookup needs.

Lesson

1. Concept and mental model

A list is an ordered, mutable collection and is useful for a sequence that changes. A tuple is ordered but immutable, which helps represent a fixed group of values. A set stores unique hashable values and provides efficient membership checks, but it does not represent a meaningful positional order. A dictionary maps unique keys to values and is the natural choice for named fields or fast lookup by identifier.

2. How it works

Choose a collection because its behavior matches the domain, not just because its syntax is familiar. Use a dictionary for a user record keyed by field names; use a list for ordered events; use a set when duplicates should disappear. Dictionary lookups can use get() with a default for optional keys, while direct indexing raises KeyError when a key is missing. That exception can be useful when a missing value indicates invalid data.

3. Use it safely and effectively

Lists and dictionaries are mutable. Assignment does not copy them: two names can refer to the same object. Use list(existing) or a suitable copy operation when an independent shallow copy is needed, and remember that nested objects remain shared in a shallow copy. For deeply nested structures, consider whether a clearer data model is more appropriate than copying. Practice by counting events by category in a dictionary and tracking unique users with a set.

4. Syntax reference

events = ['login', 'view', 'login']
counts = {}
for event in events:
    counts[event] = counts.get(event, 0) + 1
print(counts)

5. Guided walkthrough

Choose a collection based on the operations the program needs: ordered changes, fixed grouped values, uniqueness, or lookup by a meaningful key. Iterate over the collection directly, and use a dictionary method such as get when a key may not exist.

6. Common mistakes to avoid

  • Using a list for repeated membership checks when a set better represents unique values.
  • Assuming a dictionary key exists and triggering KeyError instead of defining a missing-key behavior.

Visual guide

Choose a collection

  1. Step 01 Ordered sequence

    Choose a list or fixed tuple.

  2. Step 02 Unique members

    Choose a set.

  3. Step 03 Keyed lookup

    Choose a dictionary.

Code examples

Worked example
PYTHON
events = ['login', 'view', 'login']
counts = {}
for event in events:
    counts[event] = counts.get(event, 0) + 1
print(counts)
Expected output
{'login': 2, 'view': 1}
Remove duplicates while keeping membership fast
PYTHON
tags = {'python', 'automation'}
tags.add('python')
print(len(tags))
Expected output
2
Read a dictionary value safely
PYTHON
profile = {'name': 'Mina'}
city = profile.get('city', 'Unknown')
print(city)
Expected output
Unknown

Key points

  • Select collections based on ordering, mutability, uniqueness, and lookup needs.
  • Prefer explicit, testable behavior over hidden side effects.
  • Validate assumptions at system boundaries and handle failure deliberately.

Check your understanding

Practice

Which collection is designed to hold unique values?

Your learning progress

Finish this lesson