Python · Collections

Complete Guide to Python Lists, Tuples & Sets

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.

TypeOrderedMutableAllows Duplicates
List✅ Yes✅ Yes✅ Yes
Tuple✅ Yes❌ No✅ Yes
Set❌ No✅ Yes❌ No

1. Python Lists

A list is an ordered, changeable (mutable) collection that can hold any types of elements.

1.1 Creating Lists

numbers = [1, 2, 3, 4]
fruits = ["apple", "banana", "orange"]
mixed  = [1, "hello", 3.5, True]

1.2 Accessing Elements

fruits = ["apple", "banana", "orange"]

print(fruits[0])   # apple
print(fruits[1])   # banana
print(fruits[-1])  # orange (last element)

1.3 Modifying List Items

Lists are mutable, so you can change elements by index.

fruits = ["apple", "banana", "orange"]
fruits[1] = "mango"

print(fruits)  # ["apple", "mango", "orange"]

1.4 List Slicing

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]

2. Common List Operations

2.1 Adding Elements

fruits = ["apple", "banana"]

fruits.append("orange")      # add at end
fruits.insert(1, "mango")    # insert at index 1

print(fruits)  # ["apple", "mango", "banana", "orange"]

2.2 Removing Elements

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

2.3 Looping Through a List

fruits = ["apple", "banana", "orange"]

for fruit in fruits:
    print(fruit)

2.4 Sorting Lists

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]

2.5 List Comprehension

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]

3. Copying Lists Correctly

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]

4. Python Tuples

A tuple is an ordered collection that is immutable (cannot be changed after creation). Use tuples when you want fixed data.

4.1 Creating Tuples

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

4.2 Accessing Tuple Elements

colors = ("red", "green", "blue")

print(colors[0])   # red
print(colors[-1])  # blue

4.3 Tuples Are Immutable

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")

5. Tuple Unpacking & Methods

5.1 Tuple Unpacking

point = (4, 5)

x, y = point
print(x)  # 4
print(y)  # 5

5.2 Tuple Methods

tuple1 = (1, 2, 2, 3)

print(tuple1.count(2))  # 2
print(tuple1.index(3))  # 3

Tuples are great for:

6. Python Sets

A set is an unordered collection of unique elements. Sets automatically remove duplicates and support powerful mathematical operations.

6.1 Creating Sets

numbers = {1, 2, 3, 4}
letters = {"a", "b", "c"}

Duplicates are removed automatically:

nums = {1, 2, 2, 3}
print(nums)  # {1, 2, 3}

6.2 Adding & Removing Elements

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)

6.3 Looping Through a Set

for num in numbers:
    print(num)

Note: sets do not preserve order.

7. Set Operations

Sets support mathematical operations like union, intersection, and difference.

A = {1, 2, 3}
B = {3, 4, 5}

7.1 Union (elements in A or B)

print(A | B)        # {1, 2, 3, 4, 5}
print(A.union(B))   # same result

7.2 Intersection (elements in both)

print(A & B)           # {3}
print(A.intersection(B))  # {3}

7.3 Difference (elements in A but not in B)

print(A - B)           # {1, 2}
print(A.difference(B)) # {1, 2}

7.4 Symmetric Difference (in A or B, but not both)

print(A ^ B)                  # {1, 2, 4, 5}
print(A.symmetric_difference(B))  # {1, 2, 4, 5}

8. Converting Between Lists, Tuples & Sets

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]

9. Real-World Example: Removing Duplicates

nums = [1, 2, 2, 3, 4, 4, 5]

unique_nums = list(set(nums))
print(unique_nums)

This quickly removes duplicates from a list.

10. When to Use List, Tuple, or Set?

Use CaseBest Choice
Editable, ordered collectionList
Fixed data (constants, coordinates)Tuple
Unique values, fast membership checksSet

11. Common Mistakes

MistakeWrongCorrect
Expecting order in sets
numbers = {1, 2, 3}
print(numbers[0])  # ❌
numbers = {1, 2, 3}
numbers_list = list(numbers)
print(numbers_list[0])  # ✅
Trying to modify a tuple
t = (1, 2, 3)
t[0] = 99  # ❌
t = (1, 2, 3)
lst = list(t)
lst[0] = 99
t = tuple(lst)  # ✅
Copying lists incorrectly
a = [1, 2, 3]
b = a
b[0] = 99  # changes a too
a = [1, 2, 3]
b = a.copy()   # separate copy

12. Summary

In this guide, you learned how to:

Mastering these three data structures is essential for writing effective, clean, and efficient Python code.