Arrays / Lists
๐ What are Lists? A list is an ordered collection that holds multiple values in a single variable. Instead of creating score1, score2, score3 separately you write scores = [85, 92, 78] and handle all three at once. Lists keep their order, allow duplicates, and can hold any type of value.

Appy Saysโฆ
You can't make a music app with a separate variable for every song. You need a list. Lists are how you handle collections of anything โ playlists, leaderboards, shopping carts, user feeds โ all built on lists.
What is a List?
A list stores multiple values in a single variable, in order. Each item has an index (position number), starting at 0. You can add, remove, change, and loop through items.
- โขCreate:
fruits = ["apple", "banana", "cherry"] - โขAccess by index:
fruits[0]gives"apple"(first item) - โขNegative index:
fruits[-1]gives the last item - โขChange:
fruits[1] = "mango"replaces the second item - โขAdd to end:
fruits.append("date") - โขLength:
len(fruits)gives 3
Think of it like a Spotify playlist
A playlist is a list of songs in order. Song at position 0 plays first. You can add a new song at the end (append), remove one you don't like (remove), skip to any position by index, and loop through all of them to play each one. A Python list works exactly like this.
Key List Operations
- โข
list.append(item)โ add to the end - โข
list.insert(index, item)โ add at a specific position - โข
list.remove(item)โ remove first occurrence of that value - โข
list.pop()โ remove and return the last item (or pop(index) for a specific one) - โข
list[start:end]โ slice: get a sub-list from start up to (not including) end - โข
item in listโ check if an item is in the list (True/False) - โข
list.sort()โ sort in place;sorted(list)returns a new sorted list
Real-World Examples
- โขA game leaderboard:
scores = [9800, 7200, 6400, 5100] - โขA shopping cart:
cart = ["shoes", "jacket"]โcart.append("bag")when adding - โขA TikTok feed: each video is an item in a list the algorithm fills
- โขChess moves:
moves = [("e2", "e4"), ("e7", "e5")]โ a list of pairs - โขContacts app:
contacts = [{"name": "Alex", "phone": "07..."}, ...]
Key Facts
- โขLists can hold mixed types:
[1, "hello", True, 3.14] - โขLists can hold other lists (nested):
[[1,2],[3,4]]โ useful for grids - โขSlicing:
nums[1:4]returns items at index 1, 2, 3 (not 4) - โข
nums[::-1]reverses the list โ a useful trick - โขLists are mutable (changeable) โ unlike strings or tuples
Watch Out: Index Out of Range
Accessing an index that doesn't exist gives an IndexError. If your list has 3 items (indices 0, 1, 2), trying list[3] crashes. Always check len() if you're not sure, or use negative indices for the last few items.
Remember
Indexing starts at 0, not 1. The first item is list[0], the second is list[1]. This trips up almost every beginner at least once.
What You Learned
- โขLists store multiple values in order in one variable
- โขIndex starts at 0 โ first item is
list[0] - โขAdd with
append(), remove withremove()orpop() - โขLoop through with
for item in list: - โขSlice with
list[start:end]to get a portion - โขCheck membership with
item in list
Key Facts
- โLists can hold mixed types:
[1, "hello", True, 3.14] - โLists can hold other lists (nested):
[[1,2],[3,4]]โ useful for grids - โSlicing:
nums[1:4]returns items at index 1, 2, 3 (not 4) - โ
nums[::-1]reverses the list โ a useful trick - โLists are mutable (changeable) โ unlike strings or tuples
Real-World Examples
Remember
Indexing starts at 0, not 1. The first item is list[0], the second is list[1]. This trips up almost every beginner at least once.
Quick Quiz
What is the first index in a list?