Set vs List in Python
Lists and Sets are there in Python too to store a collection of items, but they have many different behaviors, usages, and properties. So let’s go through those in detail with some explanations and example code.
1. List in Python
A List is an ordered, mutable (changeable), and indexable collection that allows duplicate elements.
Key Characteristics of a List:
- Ordered: Items are stored in a specific order and can be accessed using indexes.
- Mutable: You can modify a list (add, remove, or change items).
- Allows Duplicates: A list can have duplicate values.
- Indexing & Slicing: You can access elements using an index (e.g.,
list[0]) and slicing (list[1:4]).
Example Usage of List
# Creating a list
my_list = [10, 20, 30, 40, 50, 20]
# Printing the list
print("Original List:", my_list)
# Accessing elements by index
print("First Element:", my_list[0])
print("Last Element:", my_list[-1])
# Modifying an element
my_list[2] = 99
print("Modified List:", my_list)
# Adding an element
my_list.append(60)
print("List after append:", my_list)
# Removing an element
my_list.remove(20) # Removes the first occurrence of 20
print("List after removing 20:", my_list)
# Slicing a list
print("Sliced List (index 1 to 3):", my_list[1:4])
# Checking for duplicates
print("Count of 20 in list:", my_list.count(20))
Output:
Original List: [10, 20, 30, 40, 50, 20]
First Element: 10
Last Element: 20
Modified List: [10, 20, 99, 40, 50, 20]
List after append: [10, 20, 99, 40, 50, 20, 60]
List after removing 20: [10, 99, 40, 50, 20, 60]
Sliced List (index 1 to 3): [99, 40, 50]
Count of 20 in list: 1
2. Set in Python
A Set is an unordered, mutable collection of unique elements that doesn’t allow any form of duplication.
Key Characteristics of a List:
- Unordered: Elements are not maintained in any sequence.
- Mutable: Elements can be added and deleted but changed isn’t allowed to change any individual element
- Unique Elements: Duplicates will be eliminated from the set.
- No Indexing: It doesn’t have index and its elements can’t be accessed with any index.
Example Usage of Set
# Creating a set
my_set = {10, 20, 30, 40, 50, 20}
# Printing the set
print("Original Set:", my_set)
# Adding an element
my_set.add(60)
print("Set after adding 60:", my_set)
# Removing an element
my_set.remove(30)
print("Set after removing 30:", my_set)
# Checking if an element exists
print("Is 40 in set?", 40 in my_set)
# Performing set operations
set1 = {1, 2, 3, 4, 5}
set2 = {4, 5, 6, 7, 8}
# Union (all unique elements from both sets)
print("Union of set1 and set2:", set1 | set2)
# Intersection (common elements)
print("Intersection of set1 and set2:", set1 & set2)
# Difference (elements in set1 but not in set2)
print("Difference of set1 - set2:", set1 - set2)
# Symmetric Difference (elements in either set, but not both)
print("Symmetric Difference:", set1 ^ set2)
Output:
Original Set: {40, 10, 50, 20, 30} # Order may vary
Set after adding 60: {40, 10, 50, 20, 60, 30}
Set after removing 30: {40, 10, 50, 20, 60}
Is 40 in set? True
Union of set1 and set2: {1, 2, 3, 4, 5, 6, 7, 8}
Intersection of set1 and set2: {4, 5}
Difference of set1 - set2: {1, 2, 3}
Symmetric Difference: {1, 2, 3, 6, 7, 8}
3. Difference Between List and Set
| Feature | List | Set |
|---|---|---|
| Order | Ordered (maintains order) | Unordered (does not maintain order) |
| Duplicates | Allows duplicates | Does not allow duplicates |
| Indexing | Supports indexing and slicing | Does not support indexing |
| Mutability | Mutable (can modify elements) | Mutable (can add/remove, but not modify elements directly) |
| Performance | Searching elements is O(n) | Searching elements is O(1) on average |
| Usage | Used when order matters, allows duplicates | Used when uniqueness is required, no duplicates |
4. When to Use List vs Set?
Use a List when:
- You need to keep the order of elements.
- You need to store duplicate values.
- You need to access elements by their index.
Use a Set when:
- You need only unique values.
- You want fast lookups (checking if an item exists is faster in a set).
- Order of elements doesn’t matter.
Final Thoughts
- Use a List if you need an ordered collection that permits duplicates.
- If you need a collection of unique elements and don’t care about the order, use a Set.