1 of 25

Lists

Winter 2025

1

Adrian Salguero

2 of 25

Announcements

  • Homework 2 due tonight by 11:59pm
  • Coding Practice 2 due tonight by 11:59pm
  • Coding Practice 3 now available, due Wednesday at 11:59pm

2

3 of 25

Lists Topics for Today

  • Creating a List
  • Indexing a List
  • Slicing a List
  • List Membership
  • Modifying (Mutating) a List
    • Adding (Inserting)
    • Removing
    • Replacement
    • Rearranging

3

4 of 25

Creating a List

A list is a collection of elements.

  • Why do we need a list?
  • How to create a list in python

name_of_list = [data separated by ,]

Note: not all data have to be of the same type!

4

5 of 25

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 ,]

6 of 25

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

7 of 25

Indexing a List

7

wait_list = ["Angela", "Sofia", "Isabella", "Carl"]

8 of 25

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])

9 of 25

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

  1. ‘Angela’
  2. ‘Sofia’
  3. ‘Isabella’
  4. ‘Carl’
  5. Error: list index out of range

10 of 25

Exercise: List Indexing

What will be printed by the program below?��>>> wait_list = ["Angela", "Sofia", "Isabella", "Carl"]

>>> print(wait_list[len(wait_list)])

10

  1. ‘Angela’
  2. ‘Sofia’
  3. ‘Isabella’
  4. ‘Carl’
  5. Error: list index out of range

11 of 25

List Slicing

11

wait_list = ["Angela", "Sofia", "Isabella", "Carl", "Esther", “Doris”]

12 of 25

List Slicing

12

  • Sublist (“slicing”): mylist[start:end]
    • the sublist that starts at �index start and ends at index end – 1
    • If start is omitted: defaults to 0
    • If end is omitted: defaults to len(mylist)
    • mylist[:] and mylist[0:len(mylist)] both evaluate to the whole list

13 of 25

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)])

14 of 25

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

  1. ['John', 'Suh Young', 'Aneesha']
  2. ['John', 'Suh Young', 'Vibhav', 'Aneesha', 'Cady']
  3. ['John', 'Aneesha']
  4. ['John', 'Suh Young']

15 of 25

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

  1. ['John', 'Suh Young', 'Vibhav', 'Aneesha', 'Kellen', 'Sara']
  2. ['John', 'Suh Young', 'Vibhav', 'Aneesha', 'Cady', 'Kellen', 'Sara']
  3. ['John', 'Suh Young', 'Vibhav', 'Cady', 'Kellen', 'Sara']
  4. Error: Missing start/end indices

16 of 25

List Membership

16

wait_list = ["Angela", "Sofia", "Isabella", "Carl"]

17 of 25

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)

18 of 25

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")

19 of 25

Documentation: List Insertion

  • mylist.append(x)
    • Extend mylist by inserting x at the end
  • mylist.extend(L)
    • Extend mylist by appending all the items in the argument list L to the end of mylist
  • mylist.insert(i, x)
    • Insert item x before position i.
    • list.insert(0, x) inserts at the front of the list
    • list.insert(len(list), x) is equivalent to list.append(x)

19

Note: append, extend and insert all return None

20 of 25

Removing from a List

20

wait_list = ["Angela", "Sofia", "Isabella", "Carl"]

wait_list.remove("Isabella")

wait_list.pop(1)

21 of 25

Documentation: List Removal

  • mylist.remove(x)
    • Remove the first item from the list whose value is x
    • It is an error if there is no such item
    • Returns None

  • mylist.pop([i])
    • Remove the item at the given position in the list, and return it.
    • If no index is given, mylist.pop() removes and returns the last item in the list.

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

22 of 25

List Replacement

22

wait_list = ["Angela", "Sofia", "Isabella", "Carl"]

wait_list[3] = "Michael"

wait_list[0:2] = ["Leia", "Laura"]

23 of 25

Documentation: List Replacement

  • mylist[index] = new_value
  • mylist[start:end] = new_sublist
    • Replaces mylist[start] to mylist[end – 1] with new_sublist
    • Can change the length of the list

Examples:

  • mylist[start:end] = []
    • removes mylist[start]… mylist[end – 1]
  • mylist[len(mylist):] = L
    • is equivalent to a.extend(L)

23

24 of 25

Documentation: List Rearrangement

  • mylist.sort()
    • Sort the items of the list, in place.
    • “in place” means by modifying the original list, �not by creating a new list.
  • mylist.reverse()
    • Reverse the elements of the list, in place.

24

Note: sort and reverse return None

25 of 25

Todos for next lecture

  • Complete Homework 2 and Coding Practice 2
    • Due tonight by 11:59pm
  • Start Coding Practice 3, due Wednesday at 11:59pm
  • Ask questions on Ed!
  • Have a great weekend!

25