Lists
Think of a List as a container or a “shopping list”. It allows you to store multiple items (like numbers, words, or even other lists) in a single variable.
- Ordered: The items have a specific order that won’t change unless you change it.
- Changeable: You can add, remove, or swap items after the list is created.
- Allows Duplicates: You can have “Apple” in your list twice without any issues.
In Python, lists are written with square brackets
[]. # A list of strings
fruits = ["apple", "banana", "cherry"]
# A list of numbers
numbers = [10, 20, 30, 40]
# A mixed list (Python allows different types together!)
mixed = ["Alex", 25, True]
# Print the lists
print("Fruits:", fruits) #output: Fruits: ['apple', 'banana', 'cherry']
print("Numbers:", numbers) #output: Numbers: [10, 20, 30, 40]
print("Mixed:", mixed) #output: Mixed: ['Alex', 25, True]
Every item in a list has an “address” called an index.
- Counting starts at 0. The first item is
[0], the second is[1], and so on. - Negative indexing starts from the end.
[-1]is the last item.
colors = ["red", "blue", "green", "yellow"]
print(colors[0]) # Output: red
print(colors[-1]) # Output: yellow
print(colors[1:3]) # Output: ['blue', 'green'] (Slicing from index 1 to 2)
Lists are flexible; you can grow or shrink them as needed.
Adding Items:
.append(): Adds an item to the very end..insert(index, item): Puts an item at a specific spot..extend(): Adds another list to the end of your current list.
Removing Items:
.remove(item): Removes a specific name (e.g., “apple”)..pop(): Removes the last item (or a specific index)..clear(): Empties the entire list.
fruits = ["apple", "banana", "cherry"]
print("Initial list:", fruits)
# Adding Items
fruits.append("orange")
print("After append:", fruits) #output: ['apple', 'banana', 'cherry', 'orange']
fruits.insert(1, "grape") # Insert at index 1
print("After insert:", fruits) #output: ['apple', 'grape', 'banana', 'cherry', 'orange']
fruits.extend(["mango", "pineapple"])
print("After extend:", fruits) #output: ['apple', 'grape', 'banana', 'cherry', 'orange', 'mango', 'pineapple']
# Removing Items
fruits.remove("banana")
print("After remove:", fruits) #output: ['apple', 'grape', 'cherry', 'orange', 'mango', 'pineapple']
last_item = fruits.pop() # Removes last item
print("After pop (last item removed):", fruits) #output: ['apple', 'grape', 'cherry', 'orange', 'mango']
print("Popped item:", last_item) #output: Popped item: pineapple
second_item = fruits.pop(1) # Removes item at index 1
print("After pop at index 1:", fruits) #output: ['apple', 'cherry', 'orange', 'mango']
print("Popped item:", second_item) #output: Popped item: grape
fruits.clear()
print("After clear:", fruits) #output: []
To change an item, just refer to its index and give it a new value.
cars = ["Ford", "Volvo", "BMW"]
cars[1] = "Tesla"
print("Now the list is:", cars) # Output: Now the list is: ['Ford', 'Tesla', 'BMW']
If you want to do something to every item in a list, you use a loop.
# The "for" loop is the most common way
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print("Fruit:", x) #outputs each fruit with a label
Python can organize your list alphabetically or numerically.
.sort(): Sorts the list permanently (A-Z or 1-10)..sort(reverse=True): Sorts in descending order (Z-A)..reverse(): Simply flips the current order of the list, regardless of alphabet.
Note: Sorting usually fails if the list contains both strings and numbers!
## Sorting Alphabetically
fruits = ["banana", "apple", "cherry", "mango"]
print("Original list:", fruits) #output: ['banana', 'apple', 'cherry', 'mango']
fruits.sort()
print("After sort (A-Z):", fruits) #output: ['apple', 'banana', 'cherry', 'mango']
fruits.sort(reverse=True)
print("After sort (Z-A):", fruits) #output: ['mango', 'cherry', 'banana', 'apple']
fruits.reverse()
print("After reverse:", fruits) #output: ['apple', 'banana', 'cherry', 'mango']
## Sorting Numerically
numbers = [5, 2, 9, 1, 7]
print("\nOriginal numbers:", numbers) #output: [5, 2, 9, 1, 7]
numbers.sort()
print("After sort (ascending):", numbers) #output: [1, 2, 5, 7, 9]
numbers.sort(reverse=True)
print("After sort (descending):", numbers) #output: [9, 7, 5, 2, 1]
numbers.reverse()
print("After reverse:", numbers) #output: [1, 2, 5, 7, 9]
## Mixing Strings and Numbers
mixed = ["apple", 3, "banana", 1]
print("\nMixed list:", mixed) #output: ['apple', 3, 'banana', 1]
# Trying to sort will raise an error
try:
mixed.sort()
except TypeError as e:
print("Error when sorting mixed list:", e)
You can compare two lists to see if they are identical using
==. list1 = ["apple", "banana"]
list2 = ["apple", "banana"]
print(list1 == list2) # Output: True (because items and order match)
The easiest way to join two lists is using the
+ operator. list_a = [1, 2]
list_b = [3, 4]
combined = list_a + list_b
print("Combined list:", combined) #output: Combined list: [1, 2, 3, 4]
You cannot copy a list by simply saying
list2 = list1. If you do that, changing list2 will also change list1. Instead, use:
list2 = list1.copy()list2 = list(list1)
# Original list fruits = ["apple", "banana", "cherry"] print("Original fruits:", fruits) #output: ['apple', 'banana', 'cherry'] # ❌ Wrong way: just assigning list2 = fruits list2.append("mango") print("\nAfter modifying list2 (wrong way):") print("fruits:", fruits) #output: ['apple', 'banana', 'cherry', 'mango'] print("list2:", list2) #output: ['apple', 'banana', 'cherry', 'mango'] # ✅ Correct way: using copy() fruits = ["apple", "banana", "cherry"] list2 = fruits.copy() list2.append("mango") print("\nAfter modifying list2 (using copy):") print("fruits:", fruits) #output: ['apple', 'banana', 'cherry'] print("list2:", list2) #output: ['apple', 'banana', 'cherry', 'mango']
Here is a quick cheat sheet of the most common usage in the list.
| Method | What it does |
append() | Adds an element at the end. |
clear() | Removes all elements. |
count() | Returns the number of times a value appears. |
index() | Returns the position (index) of a value. |
pop() | Removes an item at a specific position. |
remove() | Removes the first item with a specific value. |
reverse() | Reverses the order of the list. |
sort() | Sorts the list. |
