Lists: Your First Collection

Python 3.14 · ✓ verified by execution on 2026-07-22

Lists are compound data types used to group together other values.

python
fruits = ['apple', 'banana', 'cherry']
print(fruits)
Output
['apple', 'banana', 'cherry']

Mixing Data Types

A powerful feature of Python is that lists might contain items of different types.

python
mixed_list = [1, 'apple', 3.14, True]
print(mixed_list)
Output
[1, 'apple', 3.14, True]

Changing List Contents

Unlike strings, lists are mutable. This means it is possible to change their content:

python
scores = [90, 85, 88]
scores[1] = 95
print(scores)
Output
[90, 95, 88]

Appending Items

You can add new items at the end of the list by using the append() method.

python
colors = ['red', 'green']
colors.append('blue')
print(colors)
Output
['red', 'green', 'blue']

Finding the Length

Just like with strings, the built-in len() function also applies to lists:

python
names = ['Alice', 'Bob', 'Charlie']
print(len(names))
Output
3

Iterating Over Lists

You can use a for loop to easily go through every item in a list:

python
items = ['a', 'b', 'c']
for item in items:
    print(item)
Output
a
b
c

Common Misconceptions

List indices start at 1. Python lists are zero-indexed, meaning the first element is at index 0.

You cannot change a list once it is created. Lists are mutable. You can change their elements, add new ones, or remove them.

append() can add multiple items at once. append() takes exactly one argument and adds it as a single element to the end. To add multiple elements from an iterable, use extend().

Edge Cases

Under the Hood

Watch how list modification and appending works in memory step-by-step:

python · visualize
my_list = [10, 20]
my_list[0] = 15
my_list.append(30)

Check yourself

What is the index of the first element in a Python list?

Reveal answer

0 — Python lists are zero-indexed, meaning the first element is at index 0.

Can a Python list contain both strings and numbers at the same time?

Reveal answer

Yes, lists can contain items of different types. — Lists in Python can contain items of different types within the same list.

Which method adds a single item to the end of a list?

Reveal answer

append() — The append() method adds exactly one argument as a single element to the end of the list.

Challenges

Challenge 1 +50 XP

Create a list called `animals` containing 'dog' and 'cat'. Then append 'bird' to it and print the list.

python
  • Test 1 — expects "['dog', 'cat', 'bird']\n"
Need a hint? (−25% XP)

Use the append() method on the animals list.

Show solution (0 XP)
animals = ['dog', 'cat']
animals.append('bird')
print(animals)

Challenge 2 +100 XP

Given the list `numbers = [10, 20, 30]`, change the second element (20) to 25 and print the list.

python
  • Test 1 — expects "[10, 25, 30]\n"
Need a hint? (−25% XP)

Remember that the second element is at index 1.

Show solution (0 XP)
numbers = [10, 20, 30]
numbers[1] = 25
print(numbers)