Course topics

By WebNest Studio

Python Tutorial

List vs Tuple vs Set vs Dictionary

Python's four built-in collections overlap, and choosing the right one affects correctness, readability and speed. Should a list of emails be a list or a set? Should a record be a tuple or a dict? This lesson compares lists, tuples, sets and dictionaries side by side — mutability, ordering, duplicates, access patterns and performance — with guidelines and examples for choosing.

Side-by-Side Comparison

The key properties of each collection:

  • List [1, 2] — ordered, mutable, duplicates allowed, access by index. Use for sequences that change.
  • Tuple (1, 2) — ordered, immutable, duplicates allowed, hashable. Use for fixed records and dict keys.
  • Set {1, 2} — unordered, mutable, unique items, fast membership. Use for uniqueness and set algebra.
  • Dict {"a": 1} — ordered by insertion, mutable, unique keys, fast lookup by key. Use for mappings and records with named fields.

Performance

Membership tests (x in c) are O(n) for lists and tuples but O(1) average for sets and dict keys. Appending to a list is O(1), inserting at the front is O(n) (use collections.deque). Tuples are slightly smaller and faster to create than lists.

Rules of Thumb

Ordered items that change → list. Fixed group of values → tuple (or NamedTuple/dataclass for named fields). "Is this item present?" or "remove duplicates" → set. "Look up X by Y" → dict. List vs dict: if you often search a list for an item by an id, convert it to a dict keyed by id.

Examples

The same data in four structures

Python
marks_list = [88, 72, 88, 95]
marks_tuple = (88, 72, 88, 95)
marks_set = {88, 72, 88, 95}
marks_dict = {"Asha": 88, "Ravi": 72, "Meera": 88, "Kiran": 95}

print(marks_list[0], marks_tuple[-1], sorted(marks_set), marks_dict["Kiran"])
print(len(marks_list), len(marks_tuple), len(marks_set), len(marks_dict))

marks_list.append(60)
marks_set.add(60)
marks_dict["Zoya"] = 60
print(marks_list, sorted(marks_set), marks_dict)
Output
88 95 [72, 88, 95] 95
4 4 3 4
[88, 72, 88, 95, 60] [60, 72, 88, 95] {'Asha': 88, 'Ravi': 72, 'Meera': 88, 'Kiran': 95, 'Zoya': 60}

Replacing slow list searches with a dict

Python
users = [{"id": i, "name": f"user{i}"} for i in range(1, 50_001)]

def find_in_list(user_id):
    for u in users:
        if u["id"] == user_id:
            return u

users_by_id = {u["id"]: u for u in users}

print(find_in_list(49_999)["name"])
print(users_by_id[49_999]["name"])      # O(1) instead of scanning 50,000 items
Output
user49999
user49999

Common Mistakes

  • Using a list for membership checks on large data instead of a set.
  • Using parallel lists (names[], ages[]) instead of a list of dicts or dataclasses.
  • Choosing a tuple for data that needs to change, then converting back and forth.
  • Relying on set ordering.

Key Points to Remember

  • List: ordered, mutable. Tuple: ordered, immutable. Set: unique, unordered. Dict: key → value.
  • Sets and dicts give O(1) membership/lookup; lists and tuples O(n).
  • Use tuples for fixed records and dict keys; dicts for lookups by key.
  • Choose by access pattern: by position, by membership, or by key.

Practice the examples

Change an input, predict the result, then compare it with the output. Explain why the result changes.