Why Using a set is Better Than a list for Membership
Testing in Python
The image demonstrates a Python
best practice when checking whether a value exists in a collection.
Method
1: Using a List
if username in ["Gift", "John", "Ada",
"Mike"]:
print("Allowed")
This code is perfectly valid and
works correctly. However, Python checks each element one by one until it finds
a match.
Conceptually, it works like this:
"Gift" -> Check
"John" -> Check
"Ada" -> Check
"Mike" -> Check
The larger the list becomes, the
longer the search may take.
Method 2: Using a Set (Recommended)
allowed_users = {"Gift", "John", "Ada",
"Mike"}
if username in allowed_users:
print("Allowed")
This is the recommended approach
when you only need to determine whether an item exists in a collection.
Advantages
of Using a Set
- ✅ Faster membership testing
- ✅ Automatically removes duplicate values
- ✅ Cleaner and more expressive code
- ✅ Optimized for lookup operations
List vs Set
|
List
([]) |
Set
({}) |
|
Ordered |
Unordered |
|
Allows duplicate values |
Does not allow duplicates |
|
Slower membership testing |
Very fast membership testing |
|
Supports indexing |
Does not support indexing |
Example: List Allows Duplicates
fruits = ["Apple", "Orange", "Apple"]
print(fruits)
Output:
['Apple', 'Orange', 'Apple']
Example: Set Removes Duplicates
fruits = {"Apple", "Orange", "Apple"}
print(fruits)
Output:
{'Apple', 'Orange'}
Notice that "Apple"
appears only once because sets automatically eliminate duplicate elements.
When Should You Use a List?
Use a list when:
- The order of items matters.
- You need to access elements by index.
- Duplicate values are allowed.
Example:
months = [
"January",
"February",
"March",
"April"
]
print(months[0])
Output:
January
When Should You Use a Set?
Use a set when:
- You only need to check whether an item exists.
- Fast lookup performance is important.
- The order of elements does not matter.
- Duplicate values should be removed automatically.
Example:
admins = {
"barbara",
"john",
"maria",
"david"
}
user = input("Username: ")
if user in admins:
print("Login successful")
else:
print("Access denied")
Performance Comparison
For small collections, the
performance difference is usually negligible.
For large collections containing
thousands or even millions of items:
- List membership testing: approximately O(n) (linear time)
- Set membership testing: approximately O(1) (constant time, on average)
This makes sets significantly faster
for membership checks.
Summary
Use a List ([]) when:
- The order of elements is important.
- You need indexing (items[0]).
- Duplicate values are acceptable.
Use a Set ({}) when:
- You only need to test membership using in.
- Fast lookups are required.
- Element order is unimportant.
- Duplicate values should be eliminated.
Best
Practice
Rule of thumb: If your primary operation is checking whether an item
exists in a collection (if item in collection:), prefer using a set
instead of a list whenever possible. It is generally faster, more
efficient, and communicates your intent more clearly.

No comments:
Post a Comment