By WebNest Studio Engineering Team
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.
Why it matters
The same task can take a millisecond or a minute depending on the structure that holds the data. Searching a list of a million items again and again is slow; a dictionary or a set answers the same question at once. Being able to explain the cost of an operation is what interviewers mean when they ask about time complexity.
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.
Time complexity
| Operation | Average | Worst case |
|---|---|---|
| list: read or write by index | O(1) | O(1) |
list: append | O(1) | O(n) when the list must grow |
list: x in list, insert(0, x), pop(0) | O(n) | O(n) |
| dict or set: lookup, insert, delete | O(1) | O(n) |
| tuple: read by index | O(1) | O(1) |
Examples
The same data in four structures
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)
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
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
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.
Exercise
The users are stored as a list of (id, name) pairs, so finding one by id means scanning the list. Build a dictionary from id to name and use it to print the name of user 3. Then build a set of the ids and print whether id 7 is among them.
Expected output: Zoya then False
users = [(1, "Asha"), (2, "Ravi"), (3, "Zoya")]
# TODO: build a dictionary {id: name} and print the name for id 3
# TODO: build a set of the ids and print whether 7 is in it
Show hints
dict(users)turns a list of pairs into a dictionary.- A set comprehension collects the ids:
{user_id for user_id, _ in users}.
Show solution
users = [(1, "Asha"), (2, "Ravi"), (3, "Zoya")]
names_by_id = dict(users)
print(names_by_id[3])
ids = {user_id for user_id, _ in users}
print(7 in ids)
Quick quiz
1. You need to check many times whether a value has been seen before. Which structure is best?
- A. list
- B. tuple
- C. string
- D. set
Show answer
D. Membership in a set does not depend on its size. In a list, each check scans the items.
2. Which structure suits a record that must not change, such as a latitude and longitude?
- A. tuple
- B. list
- C. set
- D. dict
Show answer
A. A tuple is ordered and immutable.
3. Why is items.insert(0, x) slow on a large list?
- A. It sorts the list
- B. Every existing item has to move one place to the right
- C. It copies the list twice
- D. It is not slow
Show answer
B. For frequent additions at the front, collections.deque is the right structure.
Interview questions
What is the time complexity of the "in" operator for a list and for a set?
Show answer
For a list it is O(n): in the worst case every element is compared. For a set or a dictionary it is O(1) on average, because the hash of the value leads directly to where it would be stored. When a membership test is repeated inside a loop, converting the list to a set first can change the whole operation from O(n²) to O(n).
How do you decide which built-in structure to use?
Show answer
Ask what the code needs. An ordered collection that changes: a list. A fixed group of values, or something to use as a key: a tuple. Uniqueness or fast membership tests: a set. Looking up a value by a key: a dictionary. If items are added and removed at both ends, use collections.deque.
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 in the playground
Write and run your own code for this topic, or start from the exercise or an example. Runs Python in your browser. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering