- Uniqueness — lists allow duplicates, sets don't
- Ordering — lists keep order, sets don't
- Performance — which one is faster for what
- Mutability — both can change
- Methods — what you can do with each
- When to use — choosing the right one
Lists vs Sets – What's the Big Difference?
If you've worked with Python for a while, you've probably used both lists and sets. They look similar, but they're built for different purposes.
The short answer is: lists keep order and allow duplicates, sets don't. But there's a lot more to it than that. Let's break it down.
💡 Here's the thing: The choice between a list and a set affects performance, memory, and how you work with your data. Choose wisely!
Uniqueness: The Biggest Difference
Lists Allow Duplicates, Sets Don't
This is the most important difference. A list can have duplicate values. A set automatically removes duplicates.
# List — can have duplicates
my_list = [1, 2, 2, 3, 3, 3, 4]
print(f"List: {my_list}") # [1, 2, 2, 3, 3, 3, 4]
print(f"List length: {len(my_list)}") # 7
# Set — automatically removes duplicates
my_set = {1, 2, 2, 3, 3, 3, 4}
print(f"Set: {my_set}") # {1, 2, 3, 4}
print(f"Set length: {len(my_set)}") # 4
# Why this matters:
# Use a list when duplicates are okay (e.g., shopping cart)
# Use a set when you need unique items (e.g., unique visitors)
What this means for you:
- Lists — keep everything, even duplicates
- Sets — only keep unique values
- Sets are great for removing duplicates
Quick Check: What happens if you put duplicates in a set? (Answer: They are automatically removed)
Ordering: Lists Keep Order, Sets Don't
Lists Are Ordered, Sets Are Unordered
Lists remember the order you put things in. Sets don't — they store items in a way that's optimized for quick lookups.
# List — preserves order
my_list = ["apple", "banana", "cherry"]
print(my_list[0]) # apple — index works!
print(my_list[1]) # banana
print(my_list[2]) # cherry
# Set — no guaranteed order
my_set = {"apple", "banana", "cherry"}
# You can't use indexes!
# print(my_set[0]) # TypeError!
# The order of a set can change
print(my_set) # {'cherry', 'apple', 'banana'} (order may vary)
# Why this matters:
# Use a list when order matters (e.g., playlist)
# Use a set when order doesn't matter (e.g., tags)
Key point:
- Lists — ordered, indexable
- Sets — unordered, not indexable
- If order matters, use a list
Quick Check: Can you access a set element by index? (Answer: No — sets are unordered)
Performance: Which Is Faster?
Sets Are Faster for Membership Testing
Sets are optimized for fast lookups. Checking if something is in a set is much faster than checking in a list, especially for large data.
import time
# Create large data
big_list = list(range(1000000))
big_set = set(range(1000000))
# Test membership in list
start = time.time()
999999 in big_list
list_time = time.time() - start
# Test membership in set
start = time.time()
999999 in big_set
set_time = time.time() - start
print(f"List membership: {list_time:.6f}s")
print(f"Set membership: {set_time:.6f}s")
# Output example:
# List membership: 0.010000s
# Set membership: 0.000001s
# Sets are MUCH faster for checking if something exists!
Performance summary:
- Sets — much faster for membership testing
- Lists — faster for iteration and ordered access
- Choose based on what you need
Quick Check: Which is faster for checking if an item exists? (Answer: Set)
Mutability: Both Can Change
Both Lists and Sets Are Mutable
Unlike tuples, both lists and sets can be changed after creation. You can add, remove, or modify elements.
# List — mutable
my_list = [1, 2, 3]
my_list[0] = 10 # Change
my_list.append(4) # Add
my_list.remove(2) # Remove
print(my_list) # [10, 3, 4]
# Set — mutable
my_set = {1, 2, 3}
my_set.add(4) # Add
my_set.remove(2) # Remove
print(my_set) # {1, 3, 4}
# Both can change, but they change differently:
# Lists keep order when changing
# Sets don't care about order
Key point:
- Both lists and sets are mutable
- Lists maintain order when modified
- Sets don't care about order
Available Methods
What You Can Do With Each
Lists and sets have different methods because they're used for different things.
# List methods
my_list = [1, 2, 3]
my_list.append(4) # Add to end
my_list.insert(1, 10) # Insert at position
my_list.pop() # Remove last
my_list.sort() # Sort
my_list.reverse() # Reverse
print(my_list) # [1, 10, 3, 4]
# Set methods
my_set = {1, 2, 3}
my_set.add(4) # Add
my_set.remove(2) # Remove
my_set.union({4, 5}) # Combine sets
my_set.intersection({3, 4}) # Find common
print(my_set) # {1, 3, 4}
# Set methods are more about set operations
# List methods are more about ordering
Key difference:
- Lists — methods for ordering (sort, reverse, insert)
- Sets — methods for set operations (union, intersection, difference)
- Choose based on what you need to do
When to Use Each
Which One Should You Choose?
# Use lists when:
# 1. Order matters
shopping_list = ["milk", "eggs", "bread"]
shopping_list.append("butter") # Adds to end
# 2. You need duplicates
scores = [85, 92, 85, 78] # Multiple 85s are fine
# 3. You need to access by index
first_score = scores[0]
# 4. You need to sort or reverse
scores.sort()
# Use sets when:
# 1. You need unique values
unique_visitors = {"alice", "bob", "charlie"}
# 2. You need fast membership testing
if "alice" in unique_visitors:
print("Alice visited!")
# 3. Order doesn't matter
tags = {"python", "tutorial", "beginner"}
# 4. You need set operations
all_tags = {"python", "java", "c++"}
python_tags = {"python", "flask"}
common = all_tags.intersection(python_tags)
Simple rule of thumb:
- Use lists — when order or duplicates matter
- Use sets — when uniqueness or fast lookups matter
- When in doubt, ask: "Does order matter?"
Quick Summary Table
| Feature | List | Set |
|---|---|---|
| Duplicates | Allowed ✅ | Not Allowed ❌ |
| Order | Preserved ✅ | Not Preserved ❌ |
| Index Access | ✅ Yes | ❌ No |
| Membership Check | Slow (O(n)) | Fast (O(1)) ✅ |
| Mutability | Mutable ✅ | Mutable ✅ |
| Memory | Smaller ✅ | Larger |
| Best For | Ordered data with duplicates | Unique values, fast lookups |
Common Mistakes
Things to Watch Out For
Using a Set When Order Matters
# WRONG — sets don't preserve order
my_set = {"apple", "banana", "cherry"}
# print(my_set[0]) # TypeError!
# CORRECT — use a list for ordered data
my_list = ["apple", "banana", "cherry"]
print(my_list[0]) # apple
Using a List for Fast Lookups
# WRONG — list membership is slow for large data my_list = list(range(1000000)) # if 999999 in my_list: # Slow! # CORRECT — use a set for fast lookups my_set = set(range(1000000)) if 999999 in my_set: # Fast!
Assuming Set Order Is Consistent
# WRONG — relying on set order
my_set = {1, 2, 3}
# The order may vary between runs!
# CORRECT — use sorted() if you need order
for item in sorted(my_set):
print(item) # 1, 2, 3
Quick Check: What's the most common mistake with sets? (Answer: Assuming they preserve order)
Try It Yourself
Compare lists and sets in the editor below. Change the code and see what happens.
LIST VS SET COMPARISON
========================================
List: [1, 2, 2, 3, 3, 3, 4, 5, 5]
Set: {1, 2, 3, 4, 5}
1. UNIQUENESS
List has 9 elements (with duplicates)
Set has 5 elements (duplicates removed)
2. ORDER
List[0]: 1 (index access works)
Set[0] would give an error!
3. PERFORMANCE (MEMBERSHIP)
List membership: 0.008000s
Set membership: 0.000001s
4. REMOVING DUPLICATES
Original: [1, 2, 2, 3, 3, 3, 4, 5, 5]
Unique: [1, 2, 3, 4, 5]
5. WHEN TO USE
Use LIST when: order matters, duplicates are okay, you need indexing
Use SET when: duplicates not allowed, fast lookups needed, order doesn't matter
List vs Set comparison complete!
You've Got It!
You now understand the key differences between lists and sets — uniqueness, ordering, performance, and when to use each. This is a common interview question, so you're ready!
Quick Quiz
Test what you've learned:
Frequently Asked Questions
Which is faster: list or set?
Can I convert a list to a set?
set(my_list). This removes all duplicates. Use list(my_set) to convert back.
Does a set preserve insertion order?
Why are sets faster for membership testing?
Can a set contain duplicate values?
What's a common interview question about lists and sets?
Where to Go From Here
Now that you understand lists and sets, check out these related topics:
List vs Dictionary
Compare lists with dictionaries for key-value data.
Learn More →Tuple vs Set
Compare tuples with sets for immutable vs unique data.
Learn More →Dictionary vs Set
Compare dictionaries with sets for key-value vs unique data.
Learn More →