By the end of this lesson, you will be able to:
:information_source: Definition: A list is a data structure that stores a collection of items. Lists are ordered (items have positions) and changeable (you can modify them).
student_1 = "Sharvin"
student_2 = "Yong"
student_3 = "Sheng"
This approach creates too many variables!
students = ["Sharvin", "Yong", "Sheng"]
Much cleaner with just one variable!
Every list has three parts:
Lists can store different types of data:
Data Type | Code Example | Output |
---|---|---|
Same Type | student = ["Sharvin", "Yong", "Sheng"] |
![]() |
Mixed Types | student_details = ["Sharvin", 20, "M"] |
![]() |
:memo: Note Important: Every item in a list has an index (position number) starting from 0. The index tells you where to find each item.
We'll explore five key list operations:
You can create a list by typing all the values:
numbers = [1,2,3,4,5,6,7,8,9,10,
11,12,13,14,15,16,17,18,19,20]
print(numbers)
Expected output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
Python provides a faster way using range()
and list()
:
numbers = list(range(1,21))
print(numbers)
Expected output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
:bulb: The ending number in
range()
is not included. This makes it easy to create lists with exact counts. For example,range(20)
gives you exactly 20 numbers (0 to 19).
Creating Lists Starting from 0
python
numbers = list(range(20)) print(numbers)
Expected output:
csharp
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
Breaking Strings into Characters
The
list()
function can also split a string into individual characters:python
chars = list("Hello, world") print(chars)
Expected output:
css
['H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd']
Creating Empty Lists
Sometimes you need to start with an empty list and add items later:
python
numbers = [] print(numbers)
Expected output:
css
[]
:mag: Access Items in List
Getting a Single Item
Use the index number in square brackets to get an item:
python
students = ["Sharvin", "Yong", "Sheng"] print(students[0])
Expected output:
Sharvin
Negative Indexing
Python lets you count from the end using negative numbers:
python
students = ["Sharvin", "Yong", "Sheng"] print(students[-1])
Expected output:
Sheng
Getting Multiple Items (Slicing)
You can get several items at once using slicing:
python
numbers = [1,4,8,2] print(numbers[1:3])
Expected output:
csharp
[4, 8]
Finding an Item's Position
Use
.index()
to find where an item is located:python
numbers = [1,4,8,2] print(numbers.index(8))
Expected output:
2
Checking if an Item Exists
Use the
in
keyword to check if something is in your list:python
numbers = [1,4,8,2] print(4 in numbers)
Expected output:
graphql
True
:pencil2: Change Item in List
Changing an item is simple - just assign a new value to that position:
python
numbers = [1,4,8,2] numbers[1] = 6 print(numbers)
Expected output:
csharp
[1, 6, 8, 2]
:emoji: Add Item in List
Method 1: append() - Add to the End
Use
.append()
to add an item at the end of your list:python
numbers = [1,4,8,2] numbers.append(3) print(numbers)
Expected output:
csharp
[1, 4, 8, 2, 3]
Method 2: insert() - Add at a Specific Position
Use
.insert()
to add an item exactly where you want it:python
numbers = [1,4,8,2] numbers.insert(2,3) print(numbers)
Expected output:
csharp
[1, 4, 3, 8, 2]
:emoji: Remove Item in List
Method 1: remove() - Remove by Value
Use
.remove()
when you know what item you want to delete:python
numbers = [1,4,8,2] numbers.remove(8) print(numbers)
Expected output:
csharp
[1, 4, 2]
Method 2: del - Remove by Position
Use
del
when you know the index of the item to remove:python
numbers = [1,4,8,2] del numbers[1] print(numbers)
Expected output:
csharp
[1, 8, 2]
:memo: Summary
Lists are powerful tools in Python that help you:
- Store multiple items in one variable
- Access items using index numbers (starting from 0)
- Modify, add, and remove items as needed
- Work with different types of data together tip Remember: Lists are like containers that can hold many items. You can think of them as a row of boxes, where each box has a number (index) and contains one item.
Code with AI: Try using AI to generate code involving lists and conditional statements.
Prompts:
Try these exercises to master lists:
Shopping List: Create a list of 5 items you need from the store. Then:
append()
remove()
Number Explorer: Create a list of numbers from 1 to 10 using range()
. Then:
Name Checker: Create a list of your friends' names. Then: