1 of 42

CSE 160 Section 4

Lists!

Grab a worksheet from the front when you come in!

CSE 160

CSE 160: Section 4

2 of 42

Logistics

  • HW2 due Jan 29

lists, loops and more

    • Submitting on Gradescope
    • Wait for autograder!

  • Written Check In due Tomorrow Jan 30
  • Programming Practice 4 due Next Wednesday Feb 4

CSE 160: Section 4

3 of 42

Lecture Review: Functions

CSE 160: Section 4

4 of 42

Writing a Function

  1. Identify a good name for the function.
  2. Identify the return value (if any).
  3. Identify any necessary parameters.
  4. Write the function definition.
  5. Write the function's docstring.
  6. Describe, on paper, in words, or in your head, the algorithm you'll use to solve the problem.
  7. Implement the function.

Test your function to see if it works

CSE 160: Section 4

5 of 42

Writing a Function

def finds_puppies_in_list(input_list):

‘’’

This function iterates through a list and finds the

number of times the string “puppies” appears

‘’’

num_puppies = 0

for i in input_list:

if i == "puppies":

num_puppies += 1

return num_puppies

list_of_pets = ["cats", "puppies", "fish", "puppies"]

result = finds_puppies_in_list(list_of_pets)

print(result)

CSE 160: Section 4

6 of 42

CSE 160: Section 4

7 of 42

Warm Up: Nested Lists

CSE 160: Section 4

8 of 42

Nested Lists

  • Nested List is a list that contains other lists as elements.

  • Think of it like a list of rows in a table:

nested[0] → [1, 2]

nested[1] → [3, 4]

  • You can access individual elements by chaining indices:

nested = [[1, 2], [3, 4], [5, 6]]

print(nested[1][0])

Output: 3

CSE 160: Section 4

9 of 42

Changing Elements in a Nested List

  • What would we need to print if we wanted to Catherine’s height?

  • What if we wanted to change Bob’s height to 90?

heights = [["Alice", 95], ["Bob", 88], ["Catherine", 76]]

print(heights[2][1])

heights[1][1] = 90

CSE 160: Section 4

10 of 42

Looping Through a Nested List

  • To loop through a nested list you’ll need a nested for loop

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

for row in matrix:

for item in row:

print(item)

Output: 1

2

CSE 160: Section 4

11 of 42

Basic List Operations

  • Make a list
    • listname = [item0, item1, …., item]
  • Access an element
    • listname[i] returns the item at index i
    • IndexError if doesn't exist
  • List Slicing
    • listname[a : b] creates a list that contains the elements in between indexes a (inclusive) and b (exclusive).

  • Length
    • len(listname) will return the number of elements in the list.

CSE 160: Section 4

12 of 42

Functions that Modify Lists

  • Append
    • listname.append(item) adds item to the end of the list
  • Insert
    • listname.insert(i, item) puts the item at index i, and moves everything else to the right
  • Extend
    • list1.extend(list2) will combine the contents of list2 to the end of list1
  • Reverse
    • listname.reverse() reverses the order of the list (in-place)
  • Sort
    • listname.sort() sorts the list in ascending order (smallest to greatest) (in-place)

CSE 160: Section 4

13 of 42

Making a List

listname = [item1, item2, ..., item2]

Example:

lst = [22, 45, 65, 89, 5]

index:

0

1

2

3

4

value:

22

45

65

89

5

CSE 160: Section 4

14 of 42

Access Items

listname[i] returns the item at index i

Example:

lst = [22, 45, 65, 89, 5]

print(lst[1])

index:

0

1

2

3

4

value:

22

45

65

89

5

Output:

45

CSE 160: Section 4

15 of 42

Changing Lists

What if we wanted to change a list?

Example:

lst = [22, 45, 65, 89, 5]

lst[1] = 10

index:

0

1

2

3

4

value:

22

10

65

89

5

CSE 160: Section 4

16 of 42

.index()

listname.index(item) returns the first index i, that the item was found at

Example:

lst = [22, 45, 65, 89, 5]

print(lst.index(45))

index:

0

1

2

3

4

value:

22

45

65

89

5

Output:

1

CSE 160: Section 4

17 of 42

List Slicing

(2 parameters)

listname[a : b] returns a new copy of the list in between indexes a and b.

  • It includes item at index a, but excludes item at the index b.
  • When a or b are left empty: ‘a’ defaults to 0, ‘b’ defaults to the last index + 1

Example:

Output:

lst = [22, 45, 65, 89, 5]

print(lst[1:3])

[45, 65]

index:

0

1

2

3

4

value:

22

45

65

89

5

CSE 160: Section 4

18 of 42

List Slicing

(3 parameters)

listname[a : b : c] returns a new copy of the list in between indexes a and b with a step size c.

  • It includes item at index a, but excludes item at the index b.
  • When c is left empty: defaults to 1

Example:

Output:

lst = [22, 45, 65, 89, 5]

print(lst[0:3:2])

[22, 65]

index:

0

1

2

3

4

value:

22

45

65

89

5

CSE 160: Section 4

19 of 42

List Slicing (Empty parameters)

lst = [22, 45, 65, 89, 5]

print(lst[:2:3])

print(lst[0::2])

print(lst[::])

Output:

[22]

[22, 65, 5]

[22, 45, 65, 89, 5]

CSE 160: Section 4

20 of 42

.insert()

listname.insert(i, item) puts the item at index i, and moves everything else to the right, in-place.

Example:

Output:

lst = [22, 45, 65, 89, 5]

lst.insert(4, 999)

print(lst)

[22, 45, 65, 89, 999, 5]

index:

0

1

2

3

4

5

value:

22

45

65

89

999

5

CSE 160: Section 4

21 of 42

.extend()

list1.extend(list2) will add list2 to the end of list1

Example:

Output:

lst1 = [1, 2, 3]

lst2 = [4, 5, 6]

lst1.extend(lst2)

print(lst1)

[1, 2, 3, 4, 5, 6]

CSE 160: Section 4

22 of 42

list + list

list1 + list2 will return a new list that has both of the lists together

Example:

Output:

lst1 = [1, 2, 3]

lst2 = [4, 5, 6]

lst3 = lst1 + lst2

print(lst3)

[1, 2, 3, 4, 5, 6]

CSE 160: Section 4

23 of 42

.reverse()

listname.reverse() reverses a list

Example:

Output:

lst = [1, 2, 3]

lst.reverse()

print(lst)

[3, 2, 1]

CSE 160: Section 4

24 of 42

.sort()

listname.sort() sorts a list

Example:

Output:

lst = [2, 1, 3]

lst.sort()

print(lst)

[1, 2, 3]

CSE 160: Section 4

25 of 42

Note About In Place Functions

insert(), extend(), reverse(), and sort() are all called in place functions.

Example:

Output:

In-place” means that the list is modified, but the result it returns is None

lst = [1, 2, 3]

result = lst.reverse()

print(result)

print(lst)

None

[3, 2, 1]

CSE 160: Section 4

26 of 42

Section Handout Problems

CSE 160: Section 4

27 of 42

Problem 2

Write a function called count(lst, value) that takes in a list and a value. It should return the number of times the value is in the list. For example: if my_lst = [1, 3, 5, 3, 2, 5, 6, 3, 3] when you call count(my_lst, 3) it should return 4.

Python Tutor

Python Tutor

CSE 160: Section 4

28 of 42

Problem 3 Dot Product

Create a function "dot_product" which takes in two lists of integers and returns the dot product of the two lists. You can assume the lists are of equal length, and contain only integer values.

So if these were your two lists:

The dot product would be:

index:

0

1

value:

1

2

index:

0

1

value:

3

4

CSE 160: Section 4

29 of 42

Problem 3 Dot Product

Create a function "dot_product" which takes in two lists of integers and returns the dot product of the two lists. You can assume the lists are of equal length, and contain only integer values.

So if these were your two lists:

The dot product would be:

1*3

index:

0

1

value:

1

2

index:

0

1

value:

3

4

CSE 160: Section 4

30 of 42

Problem 3 Dot Product

Create a function "dot_product" which takes in two lists of integers and returns the dot product of the two lists. You can assume the lists are of equal length, and contain only integer values.

So if these were your two lists:

The dot product would be:

1*3 + 4*2

index:

0

1

value:

1

2

index:

0

1

value:

3

4

CSE 160: Section 4

31 of 42

Problem 3 Dot Product

Create a function "dot_product" which takes in two lists of integers and returns the dot product of the two lists. You can assume the lists are of equal length, and contain only integer values.

So if these were your two lists:

The dot product would be:

1*3 + 4*2 = 11 Python Tutor

index:

0

1

value:

1

2

index:

0

1

value:

3

4

CSE 160: Section 4

32 of 42

Problem 3 Dot Product

Solution:

def dot_product(list1, list2):

dot_prod = 0

for num in range(len(list1)):

cur_num_1 = list1[num]

cur_num_2 = list2[num]

dot_product = dot_product + (cur_num_1 * cur_num_2)

return dot_product

index:

0

1

value:

1

2

index:

0

1

value:

3

4

CSE 160: Section 4

33 of 42

Problem 4

You and your friends each have a list containing the name of the list owner followed by the names of their best friends. Your name is "me".

my_besties = ["me", "Emily", "John", "Ed", "Louise", "Tom"]

emily_besties = ["Emily", "me", "Rob", "Sue", "Alice", "Eric"]

john_besties = ["John", "Ed", "Rob", "Sue", "Eric", "Meg", "Emily"]

louise_besties = ["Louise", "me", "Alice", "Sue", "Emily", "Meg"]

friends (a list of lists) is constructed as follows:

friends = [my_besties, emily_besties, john_besties, louise_besties]

Write a function for each of the following:

CSE 160: Section 4

34 of 42

Problem 4 Part A

Given a name and a friend list, return the index of the first occurrence of the name in the list. If the name is not in the list, the function should return -1.

def find_friend(name, friend_list):

CSE 160: Section 4

35 of 42

Problem 4 Part A

Given a name and a friend list, return the index of the first occurrence of the name in the list. If the name is not in the list, the function should return -1.

def find_friend(name, friend_list):

ONE POSSIBLE SOLUTION:

for index in range(len(friend_list)):

if friend_list[index] == name:

return index

return -1

CSE 160: Section 4

36 of 42

Problem 4 Part B

Given a name and friends (the list of friend lists), return the index of that person's list in the friends list. If that name is not found, your function should return -1.

def find_friend_list(name, friends):

CSE 160: Section 4

37 of 42

Problem 4 Part B

Given a name and friends (the list of friend lists), return the index of that person's list in the friends list. If that name is not found, your function should return -1.

def find_friend_list(name, friends):

for index in range(len(friends)):

if friends[index][0] == name:

return index

return -1

CSE 160: Section 4

38 of 42

Problem 4 Part C

Given friends,calculate and return how many of your best friends view you as one of their best friends.

def my_mutual_best_friends(friends):

CSE 160: Section 4

39 of 42

Problem 4 Part C

Given friends,calculate and return how many of your best friends view you as one of their best friends.

def my_mutual_best_friends(friends):

num_friends = 0

my_list = friends[0]

others = friends[1:]

for best_friends in others:

if "me" in best_friends:

num_friends = num_friends + 1

return num_friends

CSE 160: Section 4

40 of 42

Problem 4 Part D (If have time left)

Now, calculate the "mutual friend" value for any individual friend. Given a person’s name, return how many of that person’s best friends also view them as one of their best friends.

For example, mutual_best_friends(“John”, friends) would return 0, because none of John’s best friends include his name in their best friends list. However, mutual_best_friends(“me”, friends) would return 2 because Emily and Louise are in my best friend list and also include me in theirs.

You might find it helpful to use the helper function you wrote in part (b). def mutual_best_friends(name, friends):

CSE 160: Section 4

41 of 42

Problem 4 Part D

def mutual_best_friends(name, friends):

num_friends = 0

index = find_friend_list(name, friends) # Helper function from (b)

name_list = friends[index] # Person’s list

for friend in name_list:

other_friend_index = find_friend_list(friend, friends)

if other_friend_index != -1: # Make sure that the list exists

other_friend_list = friends[other_friend_index]

if name in other_friend_list:

num_friends = num_friends + 1

return num_friends

CSE 160: Section 4

42 of 42

Written Check-In Due Friday

CSE 160: Section 4