loop
Lists and list processing

Module 3 · Lists and list processing · Lesson 6 of 7

Lists in action

This is where it all comes together. Lists and loops are made for each other: a for loop walks a list item by item, so you can do something with every single value without ever touching an index.

One item at a time
for fruit in ["apple", "banana", "cherry"]:
print(fruit)
"apple""banana""cherry"
fruit
output
apple
banana
cherry

That pairing — loop over the list, do something with each item — is the engine behind most list work: adding them up, finding the biggest, printing a menu, building a new list from an old one. Python also gives you ready-made helpers like sum(), max() and min() for the common cases.

Run it yourselfruns in your browser · Python
main.py

One more essential tool: slicing. Where nums[2] grabs a single item, nums[1:4] grabs a whole range of them as a brand-new list.

nums[1:4]
010
120
230
340
450
↓ new list
203040

Like range, the stop 4 is excluded — you get indices 1, 2 and 3.

The two numbers are a start and a stop (stop excluded, just like range), and you can leave either out to mean “from the beginning” or “to the end.” There's even a third number for a step. Try a few:

Run it yourselfruns in your browser · Python
main.py

Indexing, slicing, looping, growing and shrinking — that's the full toolkit. A quick quiz to lock it in.