Python Lists vs Tuples vs Sets vs Dictionaries: When to Use Each
Python list vs tuple vs set vs dictionary, answered with a decision table, measured lookup timings and short examples you can run.
- List: ordered, changeable, allows duplicates. Tuple: ordered, fixed. Set: unique items, fast membership. Dict: key to value lookup.
- Checking
x ina set or dict is hash based and stays fast; on a list or tuple it scans item by item. - Tuples and frozensets are hashable, so they can be dict keys or set members; lists and sets cannot.
- Pick the structure from the question you ask most often: order, uniqueness or lookup by key.
The Python list vs tuple vs set question comes up in every beginner project, and the answer is shorter than most tutorials make it. Each structure is built for one kind of question, and dictionaries cover a fourth.
Choose well in any Python list vs tuple vs set decision and your code gets faster and clearer. Choose badly and you end up de-duplicating lists by hand or scanning a million items to check one name.
This guide compares lists, tuples, sets and dictionaries side by side, shows real timings from a quick benchmark, and finishes with a decision table plus the mistakes testers and developers make most often. If you are new to the language, start with our Python tutorial for beginners.
Python list vs tuple vs set: quick comparison
| Feature | List | Tuple | Set | Dict |
|---|---|---|---|---|
| Syntax | [1, 2] |
(1, 2) |
{1, 2} |
{"a": 1} |
| Ordered | Yes | Yes | No guaranteed order | Yes, insertion order |
| Changeable (mutable) | Yes | No | Yes | Yes |
| Duplicates | Allowed | Allowed | Not allowed | Keys unique |
| Index by position | Yes | Yes | No | By key |
| Can be a dict key | No | Yes, if items are hashable | No | No |
| Typical use | Sequence you edit | Fixed record | Unique items, membership | Lookup by key |
Dictionaries keep insertion order since Python 3.7. Sets do not promise any order, so never rely on the order you see when printing one. The Python docs describe all four in the data structures tutorial.
Lists: the default for ordered, changeable data
Use a list when you need a sequence you will add to, remove from or sort. Test data rows, API results and queued tasks are typical.
users = ["asha", "ben", "asha"]
users.append("chen")
users.sort()
print(users) # ['asha', 'asha', 'ben', 'chen']
print(users[0]) # asha
Lists are flexible, but that flexibility has a cost. Looking up a value with in checks items one by one, so it slows down as the list grows.
Tuples: a fixed record
A tuple says “these values belong together and will not change”. Coordinates, database rows and function return values fit well.
point = (12.5, 77.6)
lat, lon = point # unpacking
def min_max(nums):
return min(nums), max(nums) # returns a tuple
low, high = min_max([4, 1, 9])
Because tuples cannot change, Python can use them as dictionary keys, provided every item inside is hashable too. A tuple holding a list is not.
Tip: a one-item tuple needs a trailing comma: (5,). Without it, (5) is just the number 5.
Sets: uniqueness and fast membership
Use a set to remove duplicates or to ask “is this item present?” many times. Sets also support maths-style operations.
a = {"login", "search", "cart"}
b = {"cart", "checkout"}
print(a | b) # union
print(a & b) # intersection: {'cart'}
print(a - b) # difference
print(len(set([3, 3, 1, 1, 2]))) # 3
Note that {} creates an empty dictionary. Use set() for an empty set. If you need an immutable set, use frozenset.
Dictionaries: lookup by key
If you often ask “what is the value for this name?”, use a dict.
prices = {"apple": 1.2, "pear": 0.9}
prices["kiwi"] = 1.5
print(prices.get("mango", "n/a")) # n/a
for name, price in prices.items():
print(name, price)
Dictionaries are the backbone of JSON handling, so they appear constantly in API work; see what is an API for the request side.
Python list vs tuple vs set speed: a real timing
I ran a worst-case lookup (the last item) on a collection of 100,000 integers using Python 3.13.6 on a laptop and timeit:
| Structure | Time for 99999 in x |
|---|---|
| List | about 1,300 microseconds |
| Tuple | about 1,150 microseconds |
| Set | about 0.1 microseconds |
| Dict | about 0.04 microseconds |
Your numbers will differ, but the gap is the point: hash-based lookup is roughly four orders of magnitude faster at this size. The official time complexity page lists the average cost as O(1) for sets and dicts and O(n) for lists and tuples. Memory differs too: on the same machine, a three-item tuple used 64 bytes versus 88 for a list, and 216 bytes for a set, so small tuples are cheaper to hold in large numbers.
Python list vs tuple vs set: which one should you use?
| Your need | Use |
|---|---|
| Ordered items you will edit | List |
| Fixed group of values, function return | Tuple |
| Remove duplicates, fast “is it in there?” | Set |
| Look up a value by name or ID | Dict |
| Composite key like (x, y) | Tuple as dict key |
| Keep order and remove duplicates | list(dict.fromkeys(items)) |
Common mistakes
- Using a list for membership tests inside a loop. Convert to a set first.
- Expecting set order. Sort the result if order matters.
- Mutable default arguments.
def f(x=[])shares one list across calls; useNoneinstead. - Putting a list inside a set. It raises
TypeError: unhashable type; use a tuple. - Editing a list while looping over it. Loop over a copy or build a new list.
When you test code built on a Python list vs tuple vs set choice, a parameterised test is a handy way to check them; our guide to writing test cases shows the format.
Final thoughts
For the Python list vs tuple vs set decision, ask what you do most: edit a sequence, keep a fixed record, check membership or look up by key. Start with a list, switch to a set or dict when lookups slow you down, and use tuples for data that should not change. That rule covers most real code.
Frequently asked questions
What is the main difference between a list and a tuple in Python?
A list can be changed after creation, a tuple cannot. Tuples are therefore hashable when their items are, so they can be dictionary keys.
Are Python sets ordered?
No. Sets do not guarantee any order. Dictionaries, in contrast, keep insertion order since Python 3.7.
When should I use a set instead of a list?
Use a set when you need unique items or you check membership often. Set lookups take constant time on average, while list lookups scan every item.
Can a tuple contain a list?
Yes, but the tuple is then not hashable, and the list inside can still change. Avoid it if you need a dictionary key.
How do I remove duplicates but keep order?
Use list(dict.fromkeys(items)), which keeps the first occurrence of each item in insertion order.
Keep learning
Need help with testing, WordPress or SEO?
We help teams ship better software and grow their sites. Tell us what you are working on and we will suggest a clear next step.