Introduction to Lists in Python
ICS3U – Introduction to Computer Science
What Is a List?
A list in Python is a container that can hold many values at the same time. It is written
using square brackets:
[2, 5, 8, 10]
Lists are useful because they allow us to store and work with multiple pieces of data
without creating many separate variables.
Why Do We Need Lists?
Before lists, if we had several numbers we might write:
a = 5
b = 7
c = 12
d = 3
This is difficult to manage.
With a list:
numbers = [5, 7, 12, 3]
we can store all values in one place and process them easily.
Creating a List
A list is created with square brackets and commas:
fruits = ["apple", "banana", "orange"]
marks = [90, 85, 76, 92]
1
A list can contain:
• integers
• floats
• strings
• or a mix of different types
Accessing Elements
Python indexes start at 0. To access an element:
numbers = [10, 20, 30, 40]
print(numbers[0]) # 10
print(numbers[2]) # 30
Changing Elements in a List
We can replace any value using its index:
numbers = [10, 20, 30]
numbers[1] = 99
print(numbers) # [10, 99, 30]
Length of a List
Use len() to find how many items are in the list:
numbers = [4, 5, 6, 7]
print(len(numbers)) # 4
Creating Lists from Input
Previously, we learned how to read several numbers using split() and map().
We can use the same idea to create a list automatically:
values = list(map(float, input("Enter numbers: ").split()))
Example input:
3.5 1.2 6.8
2
Resulting list:
[3.5, 1.2, 6.8]
If the user enters values with commas:
values = list(map(float,
input("Enter numbers: ").replace(',', ' ').split()))
Simple Operations with Lists
• First item: numbers[0]
• Last item: numbers[-1]
• Add a new item: [Link](25)
• Remove an item: [Link](10)
Example:
numbers = [4, 6, 8]
[Link](10) # [4, 6, 8, 10]
[Link](6) # [4, 8, 10]
Why Lists Are Important
Lists are extremely useful because:
• They help manage many values easily.
• They work well with loops (next lesson).
• They allow us to store input data for calculations.
• They are used in nearly all real-world Python programs.
In the next lesson, we will learn how to use loops with lists to process many values at
the same time.