tips.md basics.html variables.html functions.html loops.html lists.html project.html
// lesson 5

Working With Lists

~/lessons/lists.html

A single variable holds one value. Most real problems involve a bunch of values at once — a shopping cart full of items, a set of scores, every message in a chat. A list is just a variable that holds many values in order, instead of one.

fruits = ["apple", "banana", "cherry"] print(fruits)

Square brackets, values separated by commas. That's a list of three strings. Lists can hold numbers, strings, even other lists — whatever you need.

Getting one item out

Each item in a list has a position, called an index, and indexes start at 0, not 1. This is one of the most common sources of confusion for beginners, so it's worth sitting with: the first item is index 0, the second is index 1, and so on.

fruits = ["apple", "banana", "cherry"] print(fruits[0]) # apple print(fruits[1]) # banana print(fruits[2]) # cherry

If you ask for fruits[3] here, you'll get an error, because there's no fourth item — the list only has indexes 0, 1, and 2. This kind of error is called an "index out of range" error, and you'll see it a lot when you're starting out. It almost always means you miscounted, or the list was shorter than you assumed.

Changing a list

Unlike a single number or string, a list can grow, shrink, and change after you create it. A few of the most common operations:

fruits = ["apple", "banana"] fruits.append("cherry") # add to the end print(fruits) # ["apple", "banana", "cherry"] fruits.remove("banana") # remove a specific value print(fruits) # ["apple", "cherry"] fruits[0] = "mango" # replace by index print(fruits) # ["mango", "cherry"]

Notice this is different from a normal variable. With a normal variable, you replace the whole value. With a list, you can reach in and change, add, or remove just one piece, and the rest of the list stays exactly as it was.

Looping over a list

This is where lists and loops meet, and it's probably the single most common pattern in beginner code: go through every item in a list and do something with each one.

scores = [88, 42, 95, 67, 100] for score in scores: if score >= 90: print(str(score) + " - great job") else: print(str(score) + " - keep practicing")

The loop hands you one item at a time under the name score, runs the block for that item, then moves to the next one, until it's gone through the whole list. You don't need to know how long the list is ahead of time, and you don't need to manage the index yourself — the loop takes care of all of it.

A common mistake worth knowing about early

A list starts empty or with some values, and it's tempting to build a new list by starting with an empty one and appending as you go, especially inside a loop. That pattern is extremely common and worth getting comfortable with:

numbers = [3, 7, 2, 9, 4] big_numbers = [] for n in numbers: if n > 5: big_numbers.append(n) print(big_numbers) # [7, 9]

Start with an empty list, loop through the original, and append only the values that pass some condition. This exact shape — empty list, loop, condition, append — will show up again and again once you start building actual programs, so it's worth typing out a few times until it feels natural rather than something you have to think hard about each time.