Last modified: Nov 15, 2026
Python Set vs List: Key Differences
Python gives you many built-in data structures. Two of the most common are the list and the set. At first glance, they look similar. Both store multiple items in one variable. But they behave very differently.
Picking the wrong one can slow your code down. It can also cause bugs. This guide explains the real differences. You will see clear examples and learn when to use each one.
What Is a Python List?
A list is an ordered collection of items. It keeps the order in which you add elements. It also allows duplicates. You can access items by index, starting at zero.
Lists are created with square brackets. They are mutable, so you can add, remove, or change items after creation.
# Create a list with duplicates
fruits = ["apple", "banana", "apple", "cherry"]
# Access by index
print(fruits[0])
# Add a new item
fruits.append("mango")
print(fruits)
apple
['apple', 'banana', 'apple', 'cherry', 'mango']
Notice that "apple" appears twice. The list keeps both copies. It also keeps the exact order you gave it.
What Is a Python Set?
A set is an unordered collection of unique items. It removes duplicates automatically. It does not support indexing, because there is no fixed order.
Sets are created with curly braces. They are also mutable, but the items inside must be hashable, such as numbers, strings, or tuples.
# Create a set with duplicates
fruits = {"apple", "banana", "apple", "cherry"}
# Duplicates are removed
print(fruits)
# Add a new item
fruits.add("mango")
print(fruits)
{'apple', 'banana', 'cherry'}
{'apple', 'banana', 'cherry', 'mango'}
The duplicate "apple" was dropped. The order may also change between runs. Never rely on set order.
Ordering and Duplicates
This is the biggest difference. A list preserves order and keeps duplicates. A set ignores order and removes duplicates.
If order matters to your program, use a list. If you only care about unique values, a set is often better.
# Remove duplicates while keeping order
items = [3, 1, 3, 2, 1]
# Using a set loses order
print(set(items))
# Using dict.fromkeys keeps order
print(list(dict.fromkeys(items)))
{1, 2, 3}
[3, 1, 2]
This trick works because dictionaries keep insertion order. If you work with dicts often, see this Python dictionary inversion guide for related techniques.
Performance and Speed
Speed is where sets really shine. Checking if an item exists in a set is very fast. It uses a hash table, so the lookup time is close to constant.
A list search is slower. Python must check items one by one until it finds a match. For small lists this is fine. For large data, it adds up.
You can test membership with the in operator on both types.
import time
# Large list and set
big_list = list(range(1000000))
big_set = set(range(1000000))
# Time a list lookup
start = time.time()
print(999999 in big_list)
print("List:", time.time() - start)
# Time a set lookup
start = time.time()
print(999999 in big_set)
print("Set:", time.time() - start)
True
List: 0.0125
True
Set: 0.0001
The set lookup is dramatically faster. Use a set when you check membership again and again.
Indexing and Slicing
Lists support indexing and slicing. You can grab the first item, the last item, or a range of items.
Sets do not support indexing or slicing. You cannot do my_set[0]. If you need an item, you must convert the set to a list first.
numbers = [10, 20, 30, 40]
# List slicing
print(numbers[1:3])
# Convert set to list for indexing
s = {10, 20, 30}
print(list(s)[0])
[20, 30]
10
Remember that the order of a converted set is not guaranteed. Do not depend on it.
When to Use a List
Choose a list when order matters. Choose a list when you need duplicates. Choose a list when you need indexing or slicing.
Lists are great for sequences like logs, queues, or ordered results. They also support many methods that sets lack.
- You need to keep the original order.
- You need to store repeated values.
- You need index-based access.
- You need to sort or reverse items.
If you also work with key-value data, compare structures in this Python dictionary vs list comparison.
When to Use a Set
Choose a set when you need uniqueness. Choose a set when you need fast membership tests. Choose a set when you perform math-like operations on collections.
Sets support union, intersection, and difference. These are very handy for comparing groups of items.
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}
# Set operations
print(a | b) # union
print(a & b) # intersection
print(a - b) # difference
{1, 2, 3, 4, 5, 6}
{3, 4}
{1, 2}
These operations are clean and fast. Doing the same with lists takes more code.
Memory Usage
Sets often use more memory than lists. They need extra space for the hash table. Lists store items in a simple sequence.
For small data, the difference is tiny. For millions of items, it matters. Measure before you optimize.
Common Mistakes
Beginners often mix up the two. Here are a few traps to avoid.
First, do not try to index a set. It will raise a TypeError. Second, do not assume a set keeps order. Third, do not store unhashable items like lists inside a set.
# This will fail
my_set = {1, 2, 3}
# print(my_set[0]) # TypeError
# This will also fail
# bad = {[1, 2], [3, 4]} # TypeError: unhashable
If you need a key-like lookup with order, a dictionary may be the right tool. Learn to check membership with this Python check if key exists in dict guide.
Converting Between Lists and Sets
You can convert freely. Use set() to turn a list into a set. Use list() to turn a set into a list.
This is useful for quick deduplication. Just remember that order is lost when you convert a list to a set.
data = [1, 2, 2, 3, 3, 3]
# List to set removes duplicates
unique = set(data)
print(unique)
# Set back to list
back = list(unique)
print(back)
{1, 2, 3}
[1, 2, 3]
This is one of the simplest ways to clean repeated values from a list.
Quick Comparison Table
Here is a fast summary of the main differences.
- Order: List keeps order. Set does not.
- Duplicates: List allows them. Set removes them.
- Indexing: List supports it. Set does not.
- Speed: Set is faster for lookups.
- Memory: Set usually uses more.
- Operations: Set supports union and intersection.
Keep this table in mind when you design your data flow.
Conclusion
Lists and sets are both powerful. The right choice depends on your goal. Use a list when order and duplicates matter. Use a set when you need unique values and fast lookups.
Think about what your program needs. Does it need to remember the order? Does it need to check membership often? Answer those questions and the choice becomes clear.
Practice with both. Try the examples above. Soon you will pick the right structure without thinking twice.