Home Course Roadmap
Home / Python Foundations / Lesson 19
Lesson 19 · Data Collections Beginner

Python Lists

Learn how to store, access, modify, organize, and process collections of values using one of Python's most important data structures: the list.

40–50 minutes Python Foundations Beginner
0%

Learning Objectives

By the end of this lesson, you will be able to:

  • Understand what Python lists are and why they are useful.
  • Create lists and access individual elements.
  • Modify, add, and remove list items.
  • Use indexing, slicing, loops, and membership testing.
  • Sort, reverse, and copy lists correctly.
  • Work with nested lists and real-world collections.
  • Avoid common list-related programming mistakes.
01

What Are Lists?

A list is a Python data structure used to store multiple values inside a single variable.

Instead of creating separate variables for every value, we can group related values together inside one list.

Lists are one of the most frequently used data structures in Python because they are flexible, ordered, and mutable.

Python
fruits = ["apple", "banana", "mango"]

print(fruits)
Output
['apple', 'banana', 'mango']

The square brackets [] tell Python that we are creating a list.

02

Why Lists Matter

Imagine an online store that has 1,000 products. Creating a separate variable for every product would be inefficient.

A list allows us to keep all products together:

Python
products = [
    "Laptop",
    "Keyboard",
    "Mouse",
    "Monitor"
]

We can then search, update, sort, remove, or process these values using Python.

Key Idea

Lists allow programs to work with collections of related values efficiently.

03

Creating Lists

Lists are created using square brackets.

Basic Lists
fruits = ["apple", "banana", "mango"]

numbers = [10, 20, 30, 40]

names = ["Ali", "Sara", "Ahmed"]

empty_list = []

A list can contain zero, one, or many values.

Lists Are Ordered

List elements maintain their order. The first element remains at index 0, the second at index 1, and so on.

04

Accessing List Items

We access list elements using their index.

Indexing
fruits = ["apple", "banana", "mango"]

print(fruits[0])
print(fruits[1])
print(fruits[2])
Output
apple
banana
mango

Remember: Python uses zero-based indexing.

Index Positions

For the list ["apple", "banana", "mango"]:

  • 0 → apple
  • 1 → banana
  • 2 → mango
05

Positive & Negative Indexing

Python supports both positive and negative indexes.

Positive indexes start from the beginning. Negative indexes start from the end.

Python
fruits = ["apple", "banana", "mango", "orange"]

print(fruits[0])
print(fruits[-1])
print(fruits[-2])
Output
apple
orange
mango

Negative indexing is particularly useful when you need the last few elements of a list.

06

Changing List Items

Lists are mutable. This means their elements can be changed after the list has been created.

Python
fruits = ["apple", "banana", "mango"]

fruits[1] = "orange"

print(fruits)
Output
['apple', 'orange', 'mango']

The element at index 1 was replaced.

Important

Assigning to an index replaces the existing value. It does not insert a new element.

07

Adding Items to a List

Python provides several methods for adding elements.

append()

append() adds one item to the end of a list.

append()
fruits = ["apple", "banana"]

fruits.append("mango")

print(fruits)
Output
['apple', 'banana', 'mango']

insert()

insert() adds an item at a specific position.

insert()
fruits = ["apple", "mango"]

fruits.insert(1, "banana")

print(fruits)
Output
['apple', 'banana', 'mango']

extend()

extend() adds multiple elements from another iterable.

extend()
fruits = ["apple", "banana"]

more_fruits = ["mango", "orange"]

fruits.extend(more_fruits)

print(fruits)
Output
['apple', 'banana', 'mango', 'orange']

append() vs extend()

append() adds one object as a single element, while extend() adds the elements from another iterable.

08

Removing Items

Python provides several ways to remove elements.

remove()

Removes the first matching value.

remove()
fruits = ["apple", "banana", "mango"]

fruits.remove("banana")

print(fruits)

pop()

Removes and returns an item by index. If no index is supplied, it removes the last item.

pop()
fruits = ["apple", "banana", "mango"]

removed = fruits.pop()

print(removed)
print(fruits)
Output
mango
['apple', 'banana']

del

The del statement can remove an item by index or even delete the entire list.

del
fruits = ["apple", "banana", "mango"]

del fruits[1]

print(fruits)

clear()

clear() removes all items while keeping the list itself.

clear()
fruits = ["apple", "banana", "mango"]

fruits.clear()

print(fruits)
Output
[]
09

Finding List Length

The built-in len() function tells us how many elements a list contains.

Python
fruits = ["apple", "banana", "mango"]

print(len(fruits))
Output
3

This is useful when processing lists dynamically.

10

Checking Membership

The in and not in operators allow us to check whether a value exists in a list.

Python
fruits = ["apple", "banana", "mango"]

print("apple" in fruits)
print("orange" in fruits)
print("orange" not in fruits)
Output
True
False
True

Membership testing is extremely common in real programs.

11

Looping Through Lists

A for loop allows us to process every element in a list.

Python
fruits = ["apple", "banana", "mango"]

for fruit in fruits:
    print(fruit)
Output
apple
banana
mango

This pattern is one of the most important ways to work with collections in Python.

12

Slicing Lists

List slicing allows us to extract a portion of a list.

The basic syntax is:

Syntax
list[start:stop:step]
Example
numbers = [10, 20, 30, 40, 50]

print(numbers[1:4])
print(numbers[:3])
print(numbers[2:])
print(numbers[::2])
Output
[20, 30, 40]
[10, 20, 30]
[30, 40, 50]
[10, 30, 50]
Remember

The stop index is excluded.

13

Copying Lists

Sometimes we need a separate copy of a list.

Using copy()

Python
fruits = ["apple", "banana", "mango"]

new_fruits = fruits.copy()

new_fruits.append("orange")

print(fruits)
print(new_fruits)
Output
['apple', 'banana', 'mango']
['apple', 'banana', 'mango', 'orange']

Important: Aliasing

If we write b = a, we are not creating an independent copy. Both variables refer to the same list.

Aliasing Example
a = [1, 2, 3]

b = a

b.append(4)

print(a)
Output
[1, 2, 3, 4]

Both names point to the same list object.

14

Sorting Lists

Lists can be sorted using sort().

sort()
numbers = [50, 10, 40, 20, 30]

numbers.sort()

print(numbers)
Output
[10, 20, 30, 40, 50]

Descending Order

reverse=True
numbers = [50, 10, 40, 20, 30]

numbers.sort(reverse=True)

print(numbers)
Output
[50, 40, 30, 20, 10]

sorted()

sorted() returns a new sorted list and leaves the original list unchanged.

sorted()
numbers = [30, 10, 20]

result = sorted(numbers)

print(result)
print(numbers)
Output
[10, 20, 30]
[30, 10, 20]
15

Reversing Lists

The reverse() method reverses the existing list in place.

Python
numbers = [1, 2, 3, 4, 5]

numbers.reverse()

print(numbers)
Output
[5, 4, 3, 2, 1]

reverse() vs reversed()

reverse() modifies the list itself, while reversed() returns an iterator that can be used to iterate in reverse order.

16

Nested Lists

A list can contain other lists. Such structures are called nested lists.

Nested List
matrix = [
    [1, 2],
    [3, 4]
]

print(matrix)

We can access nested values using multiple indexes.

Accessing Nested Values
matrix = [
    [1, 2],
    [3, 4]
]

print(matrix[0][1])
print(matrix[1][0])
Output
2
3

Nested lists are useful for representing tables, matrices, grids, and structured data.

17

Lists with Mixed Data Types

Python lists can contain different data types.

Python
student = [
    "Ali",
    25,
    3.75,
    True
]

print(student)
Output
['Ali', 25, 3.75, True]

Although Python allows mixed-type lists, in practical applications it is often better to keep collections logically organized.

Good Programming Practice

Use lists to represent collections where the values naturally belong together. For more structured records, dictionaries or custom classes may be more appropriate.

18

Common List Mistakes

Mistake 1: Forgetting Zero-Based Indexing

fruits = ["apple", "banana", "mango"]

print(fruits[1])

This prints banana, not apple.

Mistake 2: Using an Invalid Index

fruits = ["apple", "banana"]

print(fruits[5])

This causes an IndexError.

Mistake 3: Confusing append() and extend()

Important Difference
a = [1, 2]

a.append([3, 4])

print(a)
Output
[1, 2, [3, 4]]

The entire list [3, 4] becomes one element.

Mistake 4: Accidental Aliasing

a = [1, 2, 3]

b = a

This does not create an independent copy. Use a.copy() when you need a separate list.

19

Practical Real-World Examples

Example 1: Shopping Products

Shopping List
cart = [
    "Laptop",
    "Mouse",
    "Keyboard"
]

cart.append("Headphones")

for product in cart:
    print(product)

Example 2: Student Marks

Student Marks
marks = [78, 85, 92, 69, 88]

print("Highest:", max(marks))
print("Lowest:", min(marks))
print("Total:", sum(marks))
Output
Highest: 92
Lowest: 69
Total: 412

Example 3: Checking a User's Permission

Permissions
permissions = [
    "read",
    "write",
    "download"
]

if "write" in permissions:
    print("User can modify files.")
Output
User can modify files.
20

Practice Exercises

Exercise 1 — Create a List

Create a list containing five programming languages and print the list.

Exercise 2 — Access Items

Create a list of five cities. Print the first, third, and last city.

Exercise 3 — Modify a List

Create a list of three fruits. Replace the second fruit with another fruit.

Exercise 4 — Add Items

Create a list of three products. Use append() to add another product and insert() to add one at the beginning.

Exercise 5 — Remove Items

Create a list of five numbers and remove the third number using pop().

Exercise 6 — Sort Numbers

Create a list of numbers in random order and sort them from smallest to largest.

Exercise 7 — List Search

Ask the user for a fruit name and check whether that fruit exists in your fruit list.

21

Mini Challenge — Shopping Cart Manager

Build a simple shopping cart program using lists.

Your program should:

  • Create an empty shopping cart.
  • Add at least five products.
  • Display all products.
  • Check whether a specific product exists.
  • Remove one product.
  • Display the final number of products.
Challenge Starter
cart = []

cart.append("Laptop")
cart.append("Mouse")
cart.append("Keyboard")

# Add more products here

print("Shopping Cart:")
for product in cart:
    print("-", product)

# Check whether a product exists

# Remove a product

print("Total products:", len(cart))
Challenge Goal

Try to build the complete program yourself before looking for a solution. The goal is to practice lists, loops, membership testing, and list methods together.

22

Quick Knowledge Check

1. Which brackets are used to create a list?

2. What is the index of the first list element?

3. Which method adds one item to the end of a list?

4. Which method removes and returns an item?

5. What does len() return for a list?

6. What does fruits[-1] access?

7. Which method creates a shallow copy of a list?

8. What is the result of [1, 2, 3][1:3]?

23

Lesson Summary

Python lists are one of the most important and flexible collection types in Python.

  • Lists store multiple values in a single variable.
  • Lists use square brackets [].
  • List indexes start at 0.
  • Negative indexes access elements from the end.
  • Lists are mutable.
  • append() adds one item.
  • insert() adds an item at a specific position.
  • extend() adds multiple elements.
  • remove(), pop(), del, and clear() remove elements in different ways.
  • len() returns the number of elements.
  • in and not in perform membership testing.
  • Lists can be sliced, sorted, reversed, and copied.
  • Lists can contain other lists.
  • Assigning b = a creates an alias, not an independent copy.
Key Takeaway

Mastering lists is essential because many real Python programs work with collections of data. Lists will also become extremely important when we learn loops, functions, comprehensions, file handling, data analysis, and more advanced Python concepts.

Ready for the Next Lesson?

You've learned how Python lists store and manage collections of values.