Python List vs Tuple vs Set vs Dictionary
Updated 7 min read Aimerscrew
Python gives you four built-in collections, and choosing between a list vs tuple vs set vs dictionary is one of the first real design decisions you make as a programmer. Each one is good at different things. This lesson puts them side by side, gives you a simple flowchart for picking the right one, and shows how to convert between them.
🌍 Real-life example
Think about planning a trip. Your packing list changes as you go (a list). Your flight's GPS coordinates are a fixed pair that should never be edited by accident (a tuple). The countries you've visited only count once each (a set). Your hotel booking has named details like check-in date and room number (a dictionary).
Here is one of each, side by side:
packing = ["passport", "charger", "socks"] # list
gate_coords = (40.6413, -73.7781) # tuple
visited = {"Spain", "Japan", "Peru"} # set
booking = {"hotel": "Harbor Inn", "nights": 3} # dictionary
print(type(packing).__name__, type(gate_coords).__name__)
print(type(visited).__name__, type(booking).__name__)
list tuple
set dict
Square brackets make a list, parentheses make a tuple, and curly braces make a set or a dictionary (a dictionary has key: value pairs inside).
Comparison table: list vs tuple vs set vs dictionary
| Feature | List | Tuple | Set | Dictionary |
|---|---|---|---|---|
| Syntax | [1, 2, 3] | (1, 2, 3) | {1, 2, 3} | {"a": 1} |
| Empty | [] | () | set() | {} |
| Ordered? | Yes | Yes | No | Yes (insertion order) |
| Mutable (changeable)? | Yes | No | Yes | Yes |
| Duplicates? | Allowed | Allowed | Not allowed | Keys: no. Values: yes |
| Access items by | Index x[0] | Index x[0] | No access, only in and loops | Key x["a"] |
Lookup with in | Slow on big data (checks one by one) | Slow on big data | Very fast (hashing) | Very fast for keys |
| Can be a dict key / set item? | No | Yes (if its items are immutable) | No | No |
| Typical use | Changing sequences: to-do items, cart | Fixed records: coordinates, RGB colors | Unique items: tags, visitors | Labeled data: profiles, settings |
When should you use a list?
Use a list when order matters, items may repeat, and the collection will grow, shrink or change. Lists are the default choice for "a bunch of things".
queue = ["Ava", "Ben"]
queue.append("Cleo") # someone joins the line
first = queue.pop(0) # the first person is served
print(first, "served. Waiting:", queue)
Ava served. Waiting: ['Ben', 'Cleo']
When should you use a tuple?
Use a tuple for a small, fixed group of related values that should never change, such as a date, a color, or a pair of coordinates. Because tuples are immutable, they protect data from accidental edits and can be used as dictionary keys.
brand_color = (79, 70, 229)
red, green, blue = brand_color
print(f"rgb({red}, {green}, {blue})")
brand_color[0] = 255
rgb(79, 70, 229)
TypeError: 'tuple' object does not support item assignment
When should you use a set?
Use a set when you only care about which items exist: removing duplicates, checking membership quickly, or comparing two groups.
countries_this_year = ["Spain", "Japan", "Spain", "Peru", "Japan"]
unique = set(countries_this_year)
print(len(unique), "different countries")
print("Peru" in unique)
3 different countries
True
When should you use a dictionary?
Use a dictionary when each value has a meaningful label, and you want to look it up by that label instead of by position.
booking = {"hotel": "Harbor Inn", "nights": 3, "rate": 120}
total = booking["nights"] * booking["rate"]
print(f"{booking['hotel']}: ${total}")
Harbor Inn: $360
Which one should I use? A decision flowchart
When you're unsure, ask these questions in order:
- Do you look things up by a name or label? Use a dictionary.
- Do items have to be unique, and order doesn't matter? Use a set.
- Will the data never change after you create it? Use a tuple.
- Otherwise: use a list.
💡 Tip
Collections can be combined. A list of dictionaries is perfect for rows of records (like orders), and a dictionary of lists is great for grouping (like songs per genre). Pick each layer with the same questions.
How fast is lookup in each one?
Checking x in collection on a list or tuple means Python compares items one at a time, so the work grows with the size. Sets and dictionaries use hashing to jump straight to the right spot, so the check stays fast no matter how big they get. For a list of 10 items you'll never notice. For 10 million email addresses on a blocklist, a set is dramatically faster.
blocklist_list = ["spam@x.com", "bot@y.com", "fake@z.com"]
blocklist_set = set(blocklist_list)
email = "bot@y.com"
print(email in blocklist_list) # works, but scans item by item
print(email in blocklist_set) # same answer, found instantly
True
True
How do you convert between list, tuple, set and dictionary?
Each type has a built-in function with the same name: list(), tuple(), set() and dict(). Pass any collection in and you get the new type back.
scores = [90, 75, 90, 60]
as_tuple = tuple(scores)
as_set = set(scores)
back_to_list = sorted(as_set)
print(as_tuple)
print(as_set)
print(back_to_list)
(90, 75, 90, 60)
{90, 75, 60}
[60, 75, 90]
Converting to a set drops the duplicate 90, and sorted() turns the set back into a sorted list. Dictionaries need pairs to build from, and they give you only their keys when converted to other types:
pairs = [("gold", 1), ("silver", 2), ("bronze", 3)]
medals = dict(pairs)
print(medals)
print(list(medals))
print(list(medals.values()))
print(list(medals.items()))
names = ["Ana", "Ben"]
ages = [31, 27]
print(dict(zip(names, ages)))
{'gold': 1, 'silver': 2, 'bronze': 3}
['gold', 'silver', 'bronze']
[1, 2, 3]
[('gold', 1), ('silver', 2), ('bronze', 3)]
{'Ana': 31, 'Ben': 27}
zip() pairs up items from two lists, which is a quick way to build a dictionary. You'll see it again in built-in functions.
Real-world example: one program, four collections
orders = [ # list: orders in arrival order
{"id": 1, "item": "latte", "size": "M"}, # dict: labeled details
{"id": 2, "item": "mocha", "size": "L"},
{"id": 3, "item": "latte", "size": "S"},
]
SIZES = ("S", "M", "L") # tuple: fixed options
drinks_sold = {order["item"] for order in orders} # set: unique drinks
print("Orders today:", len(orders))
print("Different drinks:", sorted(drinks_sold))
print("Valid sizes:", SIZES)
Orders today: 3
Different drinks: ['latte', 'mocha']
Valid sizes: ('S', 'M', 'L')
The braces around order["item"] for order in orders make a set comprehension: like a list comprehension, but it builds a set.
Common mistakes when choosing a collection
1. Using a list where you need fast lookups
Checking if user in big_list inside a loop can make a program crawl. Convert once to a set and check that.
2. Expecting a set to keep order or allow indexing
my_set[0] raises TypeError: 'set' object is not subscriptable. If you need positions, use a list.
3. Writing {} for an empty set
{} is an empty dictionary. An empty set is always set().
4. Forgetting the comma in a one-item tuple
single = ("coffee")
real_tuple = ("coffee",)
print(type(single).__name__, type(real_tuple).__name__)
str tuple
Practice exercise
🏋️ Try it yourself
A music app has this play history: ["Halo", "Yellow", "Halo", "Creep", "Yellow", "Halo"]. Using the right collection for each job, print: (1) the songs in the order they were played without duplicates removed, as a tuple you can't change, (2) the number of different songs, using a set, and (3) how many times each song was played, using a dictionary.
Show solution
history = ["Halo", "Yellow", "Halo", "Creep", "Yellow", "Halo"]
locked = tuple(history)
different = len(set(history))
plays = {}
for song in history:
plays[song] = plays.get(song, 0) + 1
print(locked)
print(different, "different songs")
print(plays)
('Halo', 'Yellow', 'Halo', 'Creep', 'Yellow', 'Halo')
3 different songs
{'Halo': 3, 'Yellow': 2, 'Creep': 1}
What's next?
Congratulations, you've finished the Data Structures chapter! Next, you'll learn to organize your code into reusable blocks with Python functions.
Key takeaways
- List: ordered, changeable, allows duplicates. The default choice for a sequence of items.
- Tuple: ordered and unchangeable. Use it for fixed records like coordinates or colors.
- Set: unordered, unique items, very fast membership checks with in.
- Dictionary: key-value pairs with unique keys, ordered by insertion, fast lookup by key.
- Pick by asking: lookup by label? dict. Unique items? set. Never changes? tuple. Otherwise list.
- Convert with list(), tuple(), set() and dict(); converting to a set removes duplicates.
Frequently asked questions
What is the difference between list, tuple, set and dictionary in Python?
A list is ordered and changeable, a tuple is ordered but unchangeable, a set is unordered with unique items, and a dictionary stores key-value pairs with unique keys. Lists and tuples are accessed by index, dictionaries by key, and sets only by membership checks or loops.
Which is faster in Python, a list or a set?
For checking whether an item is present, a set is much faster on large data because it uses hashing. Lists check items one by one. For keeping order and indexing, a list is the right tool.
When should I use a tuple instead of a list?
Use a tuple when the group of values is fixed and should not change, such as a date or a pair of coordinates, or when you need to use it as a dictionary key or set item.
Is a Python dictionary ordered?
Yes. Since Python 3.7, dictionaries keep the insertion order of their keys. Sets are still unordered.
How do you convert a list to a set or dictionary in Python?
Use set(my_list) to get unique items. To make a dictionary, you need pairs, for example dict(zip(keys, values)) or dict([("a", 1), ("b", 2)]).