Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

A list is Python's built-in data structure for storing multiple values in a single variable. Lists are ordered, meaning items remain in the sequence they were added; mutable, allowing values to be changed, added, or removed after creation; and index-based, enabling elements to be accessed using positions that start at 0. Lists can store different types of data and are widely used to manage collections such as customer names, product inventories, sales records, project tasks, and many other real-world datasets.

Initialising or creating lists

# Initialise empty list data structure
empty_list = []
print("Empty List and its length:", empty_list, ",", len(empty_list))
Empty List and its length: [] , 0
# Initialise list with data with different data types
mixed_list = [1, 2.5, "Hello", True, None]
print("Mixed List:", mixed_list)
print("Mixed List length:", len(mixed_list))

# Initialize list with lists
nested_list = [[1, 2], [3, 4], [5, 6]]
print("Nested List:", nested_list)
print("Nested List length:", len(nested_list))

# Initialize list with list() constructor
constructed_list = list((1, 2, 3, 4, 5))
print("Constructed List:", constructed_list)
Student Names: ['Alice', 'Bob', 'Charlie', 'David', 'Eve']
Student Marks: [1, 2, 3, 4, 5]

List comprehensions

squares = [x**2 for x in range(10)]
print('Squares:', squares)

pairs = [(x, y) for x in [1, 2, 3] for y in [3, 1, 4] if x != y]
print('Pairs:', pairs)
Squares: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Pairs: [(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]

Accessing element using Index

# Python list stores references to objects, not the actual values directly.
mixed_list = [1, 2.5, "Hello", True, None]
print(f"first element of mixed_list: {mixed_list[0]}")
print(f"second element of mixed_list: {mixed_list[1]}")
print(f"last element of mixed_list: {mixed_list[-1]}")
print(f"second last element of mixed_list: {mixed_list[-2]}")
first element of mixed_list: 1
second element of mixed_list: 2.5
last element of mixed_list: None
second last element of mixed_list: True

Accessing elements using Loop

# accessing all elements through a loop, one by one
fruits = ['apple', 'banana', 'cherry']
for item in fruits:
    print(item)
apple
banana
cherry
# List using enumerate() to get index and value
for index, value in enumerate(fruits):
    print(f"Index: {index}, Value: {value}")
Index: 0, Value: apple
Index: 1, Value: banana
Index: 2, Value: cherry

List Methods

Each example below focuses on one list method so learners can test it in isolation.

append()

Use append() to add one item to the end of a list. Adding new customers to a CRM

extend()

Use extend() to add multiple items from another iterable. Importing vendor lists into your inventory system

insert()

Use insert() when an item must go to a specific position.

remove()

Use remove() to delete the first matching value. Firing underperforming vendors

pop()

Use pop() to remove and return an item, usually from the end. Processing last-in orders from a queue

count()

Use count() to find how many times a value appears.

index()

Use index() to locate the position of the first matching value.

sort()

Use sort() to order list values in place. Ranking sales leads by revenue potential

reverse()

Use reverse() to flip the current order of the list.

copy()

Use copy() when you want a separate list before making changes.

del statement

Quiz

Hint

Choose one answer for each question. You can still use the per-question Submit buttons for immediate feedback, then use Check total score at the end to count all marks at once.

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

append()Correct. append() adds one item to the end of a list.
extend()extend() adds multiple items from another iterable.
insert()insert() places an item at a specific position.

2. Which list method adds all items from another iterable to a list?

extend()Correct. extend() adds all elements from another iterable.
append()append() adds the entire object as a single item.
copy()copy() creates a separate copy of a list.

3. Which list method inserts an item at a specific position in a list?

insert()Correct. insert() places an item at a specified index.
append()append() always adds an item to the end.
index()index() finds the position of an existing item.

4. Which list method removes and returns the last item by default?

pop()Correct. pop() removes and returns an item, usually the last one.
remove()remove() deletes a matching value but does not return it.
reverse()reverse() changes the order of list elements.

5. Which list method returns the position of the first matching value in a list?

index()Correct. index() returns the position of the first matching item.
count()count() returns how many times a value appears.
sort()sort() orders the list values.

6. Which list method counts how many times a value appears in a list?

count()Correct. count() returns the number of occurrences of a value.
index()index() returns the position of the first matching value.
copy()copy() creates a duplicate of the list.
Check total score Reset self-check

Practice Lab

Let’s put the learning into practice.

Practice: append()

Add "marker" to the end of the list and print the updated list.

Expected output
['notebook', 'pen', 'marker']

Practice: extend()

Add "marker" and "eraser" to the list using extend() and print the result.

Expected output
['notebook', 'pen', 'marker', 'eraser']

Practice: insert()

Insert "pen" at index 1 and print the list.

Expected output
['notebook', 'pen', 'marker']

Practice: remove()

Remove "pen" from the list and print the updated list.

Expected output
['notebook', 'marker']

Practice: pop()

Remove the last item from the list using pop(). Store it in a variable called last_task and print both the removed item and the remaining list.

Expected output
publish
['draft', 'review']

Practice: count()

Count how many times "high" appears and print the result.

Expected output
2

Practice: index()

Find the index of "tablet" and print it.

Expected output
1

Practice: sort()

Sort the list in ascending order and print it.

Expected output
[12000, 12800, 13500]

Practice: reverse()

Reverse the list and print it.

Expected output
['report', 'clean', 'collect']

Practice: copy()

Create a copy of the list. Add "D" to the copy and print both lists.

Expected output
['A', 'B', 'C']
['A', 'B', 'C', 'D']

Challenge: Nested List Comprehension (Matrix Transpose)

Create a new variable called transpose using a nested list comprehension that converts rows into columns.

Expected output
Transpose: [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

Bonus Challenge: Matrix Row Sums

Use a list comprehension to create a list containing the sum of each row.

Expected output
[6, 15, 24]