Lists

Store and manage multiple values in a single variable using Python lists.

Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.

Part of the free Python course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

A list is a collection that stores multiple items in a single variable. Lists are ordered , changeable , and allow duplicate values .

Each item in a list has an index (position number). Python uses zero-based indexing — the first item is at index 0.

Lists are mutable — you can change items after creation by assigning a new value to a specific index.

Slicing lets you get a portion of a list using the syntax list[start:end] .

List comprehension is a concise way to create lists in one line. It is more advanced but very powerful once you learn it.

Lesson 7 complete — you've unlocked Python's most-used data structure!

Lists are everywhere in Python — shopping carts, user records, game inventories, search results. You can now create, access, modify, slice, sort, and loop through them.

🚀 Up next: Dictionaries — store data with named keys instead of position numbers, perfect for structured records like user profiles.

Practice quiz

Which brackets are used to create a list?

  • { }

Lists use square brackets, e.g. [1, 2, 3].

For fruits = ['apple', 'banana', 'cherry'], what is fruits[0]?

  • 'banana'
  • 'apple'
  • 'cherry'
  • Error

Answer: 'apple'. Python uses zero-based indexing, so index 0 is the first item, 'apple'.

What does fruits[-1] return?

  • The first item
  • The last item
  • An IndexError
  • The second item

Answer: The last item. Negative indexing counts from the end; -1 is the last item.

Which method adds an item to the END of a list?

  • .insert()
  • .append()
  • .extend()
  • .add()

Answer: .append(). .append(item) adds a single item to the end of the list.

What does nums[1:4] return for nums = [0, 1, 2, 3, 4, 5]?

Slicing is start-inclusive, end-exclusive: indices 1, 2, 3 give [1, 2, 3].

What does nums[::-1] produce?

  • The list reversed
  • Every 2nd item
  • The last item
  • An empty list

Answer: The list reversed. A step of -1 reverses the list.

Accessing an index that doesn't exist raises which error?

  • KeyError
  • ValueError
  • IndexError
  • TypeError

Answer: IndexError. Using an out-of-range index raises an IndexError.

What does .remove() do if the item is NOT in the list?

  • Returns None
  • Does nothing
  • Raises a ValueError
  • Adds the item

Answer: Raises a ValueError. .remove() raises a ValueError when the value isn't found.

What does sum([10, 20, 30, 40]) return?

  • 100
  • 4
  • 40
  • 25

Answer: 100. sum() adds all the numbers: 10+20+30+40 = 100.

What does [x for x in range(10) if x % 2 == 0] produce?

The comprehension keeps even numbers from 0 to 9: [0, 2, 4, 6, 8].