Python provides several built-in data structures for storing collections of values. The most common are lists, tuples, and sets. Each has different properties and use cases.
| Type | Ordered | Mutable | Allows Duplicates |
|---|---|---|---|
| List | ✅ Yes | ✅ Yes | ✅ Yes |
| Tuple | ✅ Yes | ❌ No | ✅ Yes |
| Set | ❌ No | ✅ Yes | ❌ No |
A list is an ordered, changeable (mutable) collection that can hold any types of elements.
numbers = [1, 2, 3, 4]
fruits = ["apple", "banana", "orange"]
mixed = [1, "hello", 3.5, True]
fruits = ["apple", "banana", "orange"]
print(fruits[0]) # apple
print(fruits[1]) # banana
print(fruits[-1]) # orange (last element)
Lists are mutable, so you can change elements by index.
fruits = ["apple", "banana", "orange"]
fruits[1] = "mango"
print(fruits) # ["apple", "mango", "orange"]
numbers = [1, 2, 3, 4, 5]
print(numbers[1:4]) # [2, 3, 4]
print(numbers[:3]) # [1, 2, 3]
print(numbers[2:]) # [3, 4, 5]
print(numbers[-3:]) # [3, 4, 5]
fruits = ["apple", "banana"]
fruits.append("orange") # add at end
fruits.insert(1, "mango") # insert at index 1
print(fruits) # ["apple", "mango", "banana", "orange"]
fruits = ["apple", "banana", "orange", "banana"]
fruits.remove("banana") # removes first "banana"
print(fruits) # ["apple", "orange", "banana"]
popped = fruits.pop(1) # removes element at index 1
print(popped) # "orange"
print(fruits)
del fruits[0] # delete by index
fruits.clear() # remove all elements
fruits = ["apple", "banana", "orange"]
for fruit in fruits:
print(fruit)
numbers = [5, 2, 8, 1]
numbers.sort() # ascending
print(numbers) # [1, 2, 5, 8]
numbers.sort(reverse=True) # descending
print(numbers) # [8, 5, 2, 1]
A compact way to create lists using loops in one line.
squares = [x * x for x in range(5)]
print(squares) # [0, 1, 4, 9, 16]
Assigning one list to another with = copies the reference, not the data.
a = [1, 2, 3]
b = a # both point to the same list
b[0] = 99
print(a) # [99, 2, 3]
Use copy() or list() to copy the contents:
a = [1, 2, 3]
b = a.copy() # or list(a)
b[0] = 99
print(a) # [1, 2, 3]
print(b) # [99, 2, 3]
A tuple is an ordered collection that is immutable (cannot be changed after creation). Use tuples when you want fixed data.
coordinates = (10, 20)
colors = ("red", "green", "blue")
Single-item tuple (note the comma):
one_item = (5,) # comma is required
not_a_tuple = (5) # just an integer
colors = ("red", "green", "blue")
print(colors[0]) # red
print(colors[-1]) # blue
You cannot change elements directly:
colors = ("red", "green", "blue")
# colors[0] = "yellow" # ❌ TypeError
Convert to list, modify, then convert back:
colors = ("red", "green", "blue")
colors_list = list(colors)
colors_list[0] = "yellow"
colors = tuple(colors_list)
print(colors) # ("yellow", "green", "blue")
point = (4, 5)
x, y = point
print(x) # 4
print(y) # 5
tuple1 = (1, 2, 2, 3)
print(tuple1.count(2)) # 2
print(tuple1.index(3)) # 3
Tuples are great for:
A set is an unordered collection of unique elements. Sets automatically remove duplicates and support powerful mathematical operations.
numbers = {1, 2, 3, 4}
letters = {"a", "b", "c"}
Duplicates are removed automatically:
nums = {1, 2, 2, 3}
print(nums) # {1, 2, 3}
numbers = {1, 2, 3}
numbers.add(4)
print(numbers) # {1, 2, 3, 4}
numbers.remove(2) # error if not present
numbers.discard(10) # no error if missing
print(numbers)
for num in numbers:
print(num)
Note: sets do not preserve order.
Sets support mathematical operations like union, intersection, and difference.
A = {1, 2, 3}
B = {3, 4, 5}
print(A | B) # {1, 2, 3, 4, 5}
print(A.union(B)) # same result
print(A & B) # {3}
print(A.intersection(B)) # {3}
print(A - B) # {1, 2}
print(A.difference(B)) # {1, 2}
print(A ^ B) # {1, 2, 4, 5}
print(A.symmetric_difference(B)) # {1, 2, 4, 5}
lst = [1, 2, 2, 3]
tup = tuple(lst) # list → tuple
st = set(lst) # list → set (removes duplicates)
lst2 = list(st) # set → list
print(lst) # [1, 2, 2, 3]
print(tup) # (1, 2, 2, 3)
print(st) # {1, 2, 3}
print(lst2) # [1, 2, 3]
nums = [1, 2, 2, 3, 4, 4, 5]
unique_nums = list(set(nums))
print(unique_nums)
This quickly removes duplicates from a list.
| Use Case | Best Choice |
|---|---|
| Editable, ordered collection | List |
| Fixed data (constants, coordinates) | Tuple |
| Unique values, fast membership checks | Set |
| Mistake | Wrong | Correct |
|---|---|---|
| Expecting order in sets | |
|
| Trying to modify a tuple | |
|
| Copying lists incorrectly | |
|
In this guide, you learned how to:
Mastering these three data structures is essential for writing effective, clean, and efficient Python code.