Lists
Winter 2025
1
Adrian Salguero
Announcements
2
Lists Topics for Today
3
Creating a List
A list is a collection of elements.
name_of_list = [data separated by ,]
Note: not all data have to be of the same type!
4
Code Example: Creating + Concatenating Lists
5
wait_list = ["Angela", "Sofia", "Isabella", "Carl"]
enrolled_list = ["Claire", "Julie", "Mengyi", "Cewen"]
# concatenate two lists together
all_students = enrolled_list + wait_list
print(all_students)
name_of_list = [data separated by ,]
Exercise: Lists
What is the length of the list new_students?�
students = ["Maggie", "Tyler", "Diego"]
new_students = students + ['Sean', 'James']
len(new_students)
�
A) 3 B) 4 C) 5 D) 6
6
Indexing a List
7
wait_list = ["Angela", "Sofia", "Isabella", "Carl"]
Code Example: Indexing a List
8
wait_list = ["Angela", "Sofia", "Isabella", "Carl"]
# Who is the student at the beginning of the wait list?
print(wait_list[0])
# Who is the student at the end of the wait list?
print(wait_list[len(wait_list) - 1])
Exercise: List Indexing
What will be printed by the program below?��>>> wait_list = ["Angela", "Sofia", "Isabella", "Carl"]
>>> print(wait_list[len(wait_list) - 2])
9
Exercise: List Indexing
What will be printed by the program below?��>>> wait_list = ["Angela", "Sofia", "Isabella", "Carl"]
>>> print(wait_list[len(wait_list)])
10
List Slicing
11
wait_list = ["Angela", "Sofia", "Isabella", "Carl", "Esther", “Doris”]
List Slicing
12
Code Example: Slicing a List
13
wait_list = ["Angela", "Sofia", "Isabella", "Carl", "Esther", "Doris"]
#Who are the students in the first half of the wait list?
print(wait_list[0 : len(wait_list) // 2])
#Who are the students in the second half of the wait list?
print(wait_list[len(wait_list) // 2 : len(wait_list)])
Exercise: List Slicing
What is the value of the expression below?
tas = ['John', 'Suh Young', 'Vibhav', 'Aneesha', 'Cady', 'Kellen', 'Sara']
tas[0:2] + tas[3:4]
14
Exercise: List Slicing
What is the value of the expression below?
tas = ['John', 'Suh Young', 'Vibhav', 'Aneesha', 'Cady', 'Kellen', 'Sara']
tas[:4] + tas[4:]
15
List Membership
16
wait_list = ["Angela", "Sofia", "Isabella", "Carl"]
Code Example: List Membership
17
wait_list = ["Angela", "Sofia", "Isabella", "Carl", "Esther", "Doris"]
#Is Esther in the wait list?
print("Esther" in wait_list)
#Is Sophia in the wait list?
print("Sophia" in wait_list)
#Is Solomon *not in* the wait list?
print("Solomon" not in wait_list)
#Is Isabella *not in* the wait list?
print("Isabella" not in wait_list)
Adding (Inserting) into a List
18
wait_list = ["Angela", "Sofia", "Isabella", "Carl"]
wait_list.append("Tyler")
wait_list.extend(["James", "Zane", "Maguire"])
wait_list.insert(6, "Brian")
Documentation: List Insertion
19
Note: append, extend and insert all return None
Removing from a List
20
wait_list = ["Angela", "Sofia", "Isabella", "Carl"]
wait_list.remove("Isabella")
wait_list.pop(1)
Documentation: List Removal
21
Notation from the Python Library Reference:�The square brackets around the parameter, “[i]”, means the argument is optional.
It does not mean you should type square brackets at that position.
Note: remove returns None
List Replacement
22
wait_list = ["Angela", "Sofia", "Isabella", "Carl"]
wait_list[3] = "Michael"
wait_list[0:2] = ["Leia", "Laura"]
Documentation: List Replacement
Examples:
23
Documentation: List Rearrangement
24
Note: sort and reverse return None
Todos for next lecture
25