Part 04 / Topic 1 of 11
List
A list is an ordered, changeable collection, like a shopping list you can add to and cross items off. Use square brackets []. Items can repeat. Each item has an index (its position): the first is 0, and -1 is the last item.
Real-world example: a grocery list.
groceries = ["milk", "bread", "eggs"]
print(groceries[0]) # milk
print(groceries[-1]) # eggs
print(len(groceries)) # 3
print("bread" in groceries) # True
groceries.append("rice") # add at the end
groceries.insert(1, "butter") # add at position 1
groceries.extend(["tea", "sugar"]) # add many
print(groceries)
# ['milk', 'butter', 'bread', 'eggs', 'rice', 'tea', 'sugar']
groceries.remove("eggs") # remove by value (first match)
last = groceries.pop() # remove and return the last item
print(last) # sugar
first = groceries.pop(0) # remove by position
print(first) # milk
print(groceries) # ['butter', 'bread', 'rice', 'tea']
| Method | What it does | Returns |
|---|---|---|
append(x) |
add one item at the end | None |
insert(i, x) |
add x at position i |
None |
extend(items) |
add every item from another list | None |
remove(x) |
delete first x (error if missing) |
None |
pop() / pop(i) |
delete and give back last / item at i |
the item |
sort() |
sort the list in place | None |
sorted(lst) |
make a new sorted list | new list |
index(x) / count(x) |
position of x / how many x |
int |
sort() vs sorted():
prices = [499, 99, 1299, 249]
cheap_first = sorted(prices) # new list, original unchanged
print(cheap_first) # [99, 249, 499, 1299]
print(prices) # [499, 99, 1299, 249]
prices.sort(reverse=True) # changes the list itself
print(prices) # [1299, 499, 249, 99]
names = ["ravi", "Meena", "asha"]
print(sorted(names)) # ['Meena', 'asha', 'ravi'] (capitals sort first)
print(sorted(names, key=str.lower)) # ['asha', 'Meena', 'ravi'] (ignore case)
Handy built-ins: these functions work on a list (and on tuples, sets and other collections too).
| Function | What it gives back | Example with marks = [67, 92, 45, 88] |
|---|---|---|
len(x) |
how many items | len(marks) → 4 |
sum(x) |
total of numbers | sum(marks) → 292 |
min(x) / max(x) |
smallest / largest item | min(marks) → 45, max(marks) → 92 |
sorted(x) |
a new sorted list | sorted(marks) → [45, 67, 88, 92] |
marks = [67, 92, 45, 88]
average = sum(marks) / len(marks)
print(f"Average: {average}") # Average: 73.0
print(sorted(marks, reverse=True)) # [92, 88, 67, 45]
key=str.lowertellssortedto compare the lowercase version of each name. You will learn more about passing functions like this in Part 5.
🔍 Going deeper: a list stores references to objects, not the objects themselves, so one list can hold mixed types. Adding or removing at the end (
append,pop()) is fast;insert(0, x)andpop(0)must shift every item, so they get slow on very big lists.sort()is stable: items that compare equal keep their original order.
Read more: the official tutorial's data structures chapter.
Done here? The practice set for Part 4 — 8 exercises with solutions — is on the part page.