List: Store and Manage Multiple Items

Introduction

In this chapter, you will learn Python lists, one of the most frequently used data structures in real development. Lists let you store multiple values in order and update them when needed. Once you master lists, you can handle collections like user names, scores, products, and tasks efficiently.

Prerequisites

  • Python 3.10+ installed
  • Basic understanding of variables, strings, loops, and keywords
  • Ability to run .py files in terminal or IDE

What Is a List

A list is an ordered, mutable collection of items.

Key points:

  • Ordered: items keep their position
  • Mutable: you can add, remove, and modify items
  • Flexible: items can be different data types
python
# Create a basic list
fruits = ["apple", "banana", "orange"]
 
# Print the list
print(fruits)

1) Create and Access List Items

Use square brackets [] to create lists and index positions to access items.

python
# Create list
colors = ["red", "green", "blue"]
 
# Access by index
print(colors[0])   # red
print(colors[2])   # blue
print(colors[-1])  # blue

Warning

List indexing starts at 0.
Accessing an invalid index raises IndexError.

2) Modify List Items

Because lists are mutable, you can update elements directly.

python
# Original list
scores = [80, 75, 90]
 
# Update second item
scores[1] = 85
 
# Print updated list
print(scores)  # [80, 85, 90]

3) Add and Remove Items

Common list operations:

  • append() add one item to end
  • insert() add at specific position
  • remove() remove by value
  • pop() remove by index (or last item)

4) Useful List Functions and Methods

Length

python
# Count list items
numbers = [10, 20, 30, 40]
print(len(numbers))  # 4

Sort

python
# Sort list in ascending order
prices = [19, 5, 12, 7]
prices.sort()
print(prices)  # [5, 7, 12, 19]

Reverse

python
# Reverse current order
letters = ["a", "b", "c"]
letters.reverse()
print(letters)  # ['c', 'b', 'a']

Membership Check

python
# Check if item exists
users = ["tom", "amy", "leo"]
print("amy" in users)  # True

Tip

Best Practice

Use descriptive list names like student_scores or shopping_cart so code reads like plain English.

5) Loop Through a List

Lists are often used with for loops.

python
# List of product names
products = ["mouse", "keyboard", "monitor"]
 
# Print each product
for product in products:
    print(product)

Loop with index and value:

python
# List with values
levels = ["beginner", "intermediate", "advanced"]
 
# Print index and value together
for idx, level in enumerate(levels):
    print(idx, level)

6) Real Mini Example: Shopping Cart Summary

This mini example uses list operations and looping for a practical task.

This pattern appears in e-commerce, to-do apps, and menu systems.

Common Beginner Mistakes

Mistake 1: Confusing append() and extend()

append() adds one item, while extend() adds items from another iterable.

Mistake 2: Removing Items While Looping Incorrectly

Directly removing items during iteration can skip elements unexpectedly.

Mistake 3: Assuming List Copy Is Independent

new_list = old_list points to the same list. Use old_list.copy() for a shallow copy.

Surprise Practice Challenge

Build a tiny "Top 3 Goals Tracker":

  1. Ask user to input 3 goals
  2. Store goals in a list
  3. Print them as numbered lines
  4. Ask which goal is completed
  5. Remove completed goal and print remaining goals

Reference implementation:

FAQ

Can a list store mixed data types?

Yes. A list can contain strings, numbers, booleans, and even other lists.

What is the difference between pop() and remove()?

remove(value) deletes by value, while pop(index) deletes by position and returns the removed item.

How do I copy a list safely?

Use new_list = old_list.copy() or slicing new_list = old_list[:].

Should I always sort the original list?

Not always. If you need to keep original order, use sorted(list_name) to create a new sorted list.