Lists
Lists
Sometimes one variable is not enough.
You may want to store:
- Many names
- Many numbers
- Many products
- Many tasks
Creating many separate variables becomes hard to manage.
Python gives us lists to solve this problem.
What Is a List?
A list is a collection of values stored in one variable.
Lists use square brackets [].
names = ["Tom", "Sara", "Mike"]
print(names)
Output
['Tom', 'Sara', 'Mike']
Why Lists Matter
Lists help you:
- Store many values together
- Keep data organized
- Add new items
- Remove items
- Change items
- Search data
- Loop through items easily
Creating Lists
numbers = [10, 20, 30]
colors = ["Red", "Blue", "Green"]
mixed = ["Book", 10, True]
A list can store text, numbers, booleans, or mixed values.
Reading Items with Indexes
Each item has a position called an index.
The first item starts at 0.
fruits = ["Apple", "Banana", "Orange"]
print(fruits[0])
print(fruits[1])
print(fruits[2])
Output
Apple
Banana
Orange
Negative Indexes
Negative indexes count from the end.
-1last item-2second last item
print(fruits[-1])
print(fruits[-2])
Output
Orange
Banana
Updating Items
Lists can be changed.
fruits = ["Apple", "Banana", "Orange"]
fruits[1] = "Grape"
print(fruits)
Output
['Apple', 'Grape', 'Orange']
Adding Items with append()
Adds one item to the end.
fruits = ["Apple", "Banana"]
fruits.append("Orange")
print(fruits)
Output
['Apple', 'Banana', 'Orange']
Adding Items with insert()
Adds an item at a chosen position.
fruits = ["Apple", "Orange"]
fruits.insert(1, "Banana")
print(fruits)
Output
['Apple', 'Banana', 'Orange']
Adding Many Items with extend()
Adds multiple items.
fruits = ["Apple"]
fruits.extend(["Banana", "Orange"])
print(fruits)
Output
['Apple', 'Banana', 'Orange']
Removing with remove()
Removes by value.
fruits = ["Apple", "Banana", "Orange"]
fruits.remove("Banana")
print(fruits)
Output
['Apple', 'Orange']
Removing with pop()
Removes by index.
fruits = ["Apple", "Banana", "Orange"]
fruits.pop(1)
print(fruits)
Output
['Apple', 'Orange']
Clearing a List
Removes all items.
items = ["Pen", "Book"]
items.clear()
print(items)
Output
[]
Finding Item Position
Use index().
fruits = ["Apple", "Banana", "Orange"]
print(fruits.index("Banana"))
Output
1
Counting Items
Use count().
numbers = [1, 2, 2, 3]
print(numbers.count(2))
Output
2
Sorting Items
Use sort().
numbers = [3, 1, 2]
numbers.sort()
print(numbers)
Output
[1, 2, 3]
Reversing Items
Use reverse().
numbers = [1, 2, 3]
numbers.reverse()
print(numbers)
Output
[3, 2, 1]
What Is Slicing?
Slicing means taking part of a list.
Basic format:
list[start:stop:step]
start= where to beginstop= where to stop (not included)step= how many positions to move
Use this list:
fruits = ["Apple", "Banana", "Orange", "Grape", "Mango"]
Slicing with Start and Stop
print(fruits[1:4])
Output
['Banana', 'Orange', 'Grape']
From Beginning
print(fruits[:3])
Output
['Apple', 'Banana', 'Orange']
To the End
print(fruits[2:])
Output
['Orange', 'Grape', 'Mango']
Copy a List
print(fruits[:])
Output
['Apple', 'Banana', 'Orange', 'Grape', 'Mango']
Using Step
Take every second item.
print(fruits[::2])
Output
['Apple', 'Orange', 'Mango']
Start, Stop, Step Together
print(fruits[1:5:2])
Output
['Banana', 'Grape']
Reverse with Slicing
print(fruits[::-1])
Output
['Mango', 'Grape', 'Orange', 'Banana', 'Apple']
Looping Through a List
names = ["Tom", "Sara", "Mike"]
for name in names:
print(name)
Output
Tom
Sara
Mike
List Length
Use len().
items = ["Pen", "Book", "Bag"]
print(len(items))
Output
3
Code Along
cities = ["New York", "Toronto", "Chicago"]
cities.append("Vancouver")
for city in cities:
print(city)
Output
New York
Toronto
Chicago
Vancouver
Mini Challenge
Build a shopping list.
Steps:
- Create a list with:
Milk,Bread,Eggs - Add
Juice - Remove
Bread - Print the first two items using slicing
- Print the final list
Expected output:
['Milk', 'Eggs']
['Milk', 'Eggs', 'Juice']
Real World Use Case
Programs use lists for shopping carts, contact lists, tasks, products, and storing many records.
Quiz
- What is a list?
- What index number is the first item?
- What does
append()do? - What does slicing do?
Assignment
Create a list of 5 favorite movies. Add one movie, remove one movie, then print the first three items using slicing.
Summary
You learned how to create lists, read items, update data, remove data, use methods, slice lists, and loop through list values.