All lessons

Lists

7 min · Python

Hold many values under one name, and reach for the one you want.

Python · Lists0/5 cleared

Enemy 1 of 5

This code makes a list called fruits that holds three fruit names in order, then prints the whole list. Press Run to see it.

Why this works

So far every variable has held exactly one value. A list changes that: it holds several values in order, all under one name, inside square brackets and separated by commas. Think of it as a single labeled shelf with multiple slots, instead of one labeled box.

Lists are one of Python's most-used features because so many real problems are naturally "a bunch of things": a leaderboard of scores, a roster of players, the lines of a file. Rather than creating fruit1, fruit2, fruit3 as separate variables (which quickly becomes unmanageable), you keep them together in one list and can loop over, sort, search, or grow it as a whole.

Run this to make a list of fruits and print the whole thing.

You
Syntax Skeleton
Loading editor…
Target output: ['apple', 'banana', 'cherry']