Free lab Real Python 3. Zero installs. Your code stays in this browser. Open the playground

Lists: Your First Collection

12 min 20 XP
Study loop Read Predict Run Tweak Prove Review
Lesson 1 of 6 · View course roadmap
Step 1

Learn the idea

A list stores an ordered collection of items — any types, any length, changeable at any time:

  • fruits = ["apple", "banana", "cherry"]
  • fruits[0]"apple" (index from 0), fruits[-1] → last item
  • fruits.append("kiwi") — add to the end
  • len(fruits) — how many items
  • "apple" in fruitsTrue — membership test

Loop over a list directly — no indexes needed: for fruit in fruits:. This is the Pythonic way.

Where you'll use this

API responses arrive as lists of records; every table, feed and search result you render starts life as a list. append() inside a loop is the fundamental 'collect results' pattern of data processing.

Common mistakes

  • IndexError: list index out of range — a 4-item list has indexes 0–3, and an empty list has none at all.
  • Copy confusion: b = a makes both names point to the same list; changing one changes 'both'. Real copy: b = a.copy() or b = a[:].
  • append() vs extend(): append([4,5]) adds one nested list; extend([4,5]) adds two numbers.

Pro tip

Lists have a fast membership test but for thousands of 'x in collection' checks, convert to a set first — it's hundreds of times faster on large data.

Step 2

Watch how it runs — line by line


        

Press play to watch Python execute this code, one line at a time.

Step 3

Try it yourself

Blank · autosaved

The lesson example is loaded and ready — press Run, then change something and run it again. Breaking it is part of learning. Want a clean slate? Tap “New blank”.

PYexample.py
+ Enter to run
Output appears here…
Step 4

Pass the challenge +20 XP

Blank · autosaved

Create a list langs containing "Python", "SQL" and "JavaScript". Append "Rust", then print the list's length and its last item.

Target output
4
Rust
PYchallenge.py
Run your code to check it…
Step 5

Check your understanding

1. What is nums[-1] for nums = [4, 5, 6]?
2. Which method adds an item to the end of a list?
Last step

Your notes (saved on this device)

Tip: use and to move between lessons, K to search everything.

Your next ten minutes

Write Python that does something useful.

Start free. No install, no card, no passive video marathon.

Start learning free → Explore the path