Python Basics · Lesson 7
Lists
Learn how Python lists store multiple values, access items, modify data, add and remove elements, sort information and process collections of data.
What you will learn
- • Creating lists
- • List indexing
- • Negative indexing
- • Updating list items
- • append()
- • insert()
- • remove()
- • pop()
- • len()
- • sort() and reverse()
- • Membership with in
- • Processing lists
Part 1
What is a list?
A list is a collection that can store multiple values in a single variable. Lists are useful when working with groups of related data such as student names, product prices or sales amounts.
students = ["Aman", "Riya", "Rahul"]
print(students)CHECK YOUR UNDERSTANDING
Which brackets are used to create a Python list?
Complete the list: students = _____
Lists can contain multiple values
A list can contain several values, including strings, numbers and other Python data types.
prices = [500, 750, 1200, 900]
print(prices)CHECK YOUR UNDERSTANDING
How many values are stored in prices?
List indexing
Like strings, Python lists use indexes starting from 0.
colors = ["red", "green", "blue"]
print(colors[0])
print(colors[1])
print(colors[2])CHECK YOUR UNDERSTANDING
What does colors[0] return?
CHECK YOUR UNDERSTANDING
What does colors[2] return?
Negative indexing
Negative indexes allow you to access items from the end of the list. Index -1 refers to the last item.
numbers = [10, 20, 30, 40]
print(numbers[-1])CHECK YOUR UNDERSTANDING
What does numbers[-1] return?
Important
Changing list items
Lists are mutable, which means their existing items can be changed.
prices = [100, 200, 300]
prices[1] = 250
print(prices)CHECK YOUR UNDERSTANDING
What will the final list be?
Change the first item: prices[_____] = 999
append()
The append() method adds one item to the end of a list.
students = ["Aman", "Riya"]
students.append("Rahul")
print(students)CHECK YOUR UNDERSTANDING
What is the final list?
Complete: students._____("Neha")
insert()
The insert() method adds an item at a specific index.
students = ["Aman", "Rahul"]
students.insert(1, "Riya")
print(students)CHECK YOUR UNDERSTANDING
What is the final list?
Complete: products._____(0, "Laptop")
remove()
The remove() method removes the first matching value from a list.
items = ["Pen", "Book", "Bag"]
items.remove("Book")
print(items)CHECK YOUR UNDERSTANDING
What is the final list?
pop()
The pop() method removes and returns an item. Without an index, it removes the last item.
numbers = [10, 20, 30]
last = numbers.pop()
print(last)
print(numbers)CHECK YOUR UNDERSTANDING
What value is stored in last?
CHECK YOUR UNDERSTANDING
What is the list after numbers.pop()?
len() with lists
The len() function returns the number of items in a list.
students = ["Aman", "Riya", "Rahul", "Neha"]
print(len(students))CHECK YOUR UNDERSTANDING
What will len(students) return?
Complete: total = _____(prices)
sort()
The sort() method arranges list items in ascending order by default.
numbers = [50, 10, 40, 20]
numbers.sort()
print(numbers)CHECK YOUR UNDERSTANDING
What is the sorted list?
Complete: numbers._____()
reverse()
The reverse() method reverses the current order of items in a list.
numbers = [10, 20, 30]
numbers.reverse()
print(numbers)CHECK YOUR UNDERSTANDING
What is the result?
Checking membership with in
The in operator checks whether a value exists in a list.
courses = ["Python", "SQL", "Excel"]
print("Python" in courses)CHECK YOUR UNDERSTANDING
What will "Python" in courses return?
CHECK YOUR UNDERSTANDING
What will "Java" in courses return?
Predict the output
Think before running the code
numbers = [5, 10, 15]
numbers.append(20)
print(len(numbers))
print(numbers[-1])CHECK YOUR UNDERSTANDING
What will the two print statements display?
Debug the list code
students = ["Aman", "Riya"]
students.append["Rahul"]
print(students)CHECK YOUR UNDERSTANDING
What is wrong with the append statement?
Data Analytics Connection
Lists and data
Lists are one of the first Python structures you will use to represent collections of data. For example, sales values can be stored in a list and then processed.
sales = [12000, 15000, 9000, 18000]
sales.sort()
print(sales)CHECK YOUR UNDERSTANDING
After sorting, which value appears first?
Practice
Complete the list operations
Add an item to the end: names._____("Aman")
Add an item at a position: names._____(1, "Aman")
Remove a value: names._____("Aman")
Remove and return the last item: names._____()
Arrange numbers: numbers._____()
Count list items: total = _____(numbers)
Mini Challenge
Manage a product list
Start with three products. Add a new product, remove one existing product and then print the final list.
products = ["Laptop", "Mouse", "Keyboard"]
products.append("Monitor")
products.remove("Mouse")
print(products)Expected output:
["Laptop", "Keyboard", "Monitor"]Final Challenge
Analyze sales values
Create a list containing five sales values. Find the number of sales, sort the values and access the highest value using an appropriate list operation.
sales = [45000, 12000, 78000, 35000, 56000]
sales.sort()
print(len(sales))
print(sales)
print(sales[-1])Experiment by adding more sales values and observe how the results change.
Lesson recap
- ✓ Lists store multiple values.
- ✓ List indexes start at 0.
- ✓ -1 refers to the last item.
- ✓ Lists can be modified.
- ✓ append() adds an item at the end.
- ✓ insert() adds an item at a specific position.
- ✓ remove() removes a matching value.
- ✓ pop() removes and returns an item.
- ✓ len() counts list items.
- ✓ sort() arranges items.
- ✓ reverse() reverses the current order.
- ✓ in checks whether a value exists in a list.