CSE 160 Section 4
Lists!
Grab a worksheet from the front when you come in!
CSE 160
CSE 160: Section 4
Logistics
lists, loops and more
CSE 160: Section 4
Lecture Review: Functions
CSE 160: Section 4
Writing a Function
Test your function to see if it works�
�
CSE 160: Section 4
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
Demo on Ed: post on Lists lecture
CSE 160: Section 4
Warm Up: Nested Lists
CSE 160: Section 4
Nested Lists
nested[0] → [1, 2]
nested[1] → [3, 4]
nested = [[1, 2], [3, 4], [5, 6]]
print(nested[1][0])
Output: 3
CSE 160: Section 4
Changing Elements in a Nested List
heights = [["Alice", 95], ["Bob", 88], ["Catherine", 76]]
print(heights[2][1])
heights[1][1] = 90
CSE 160: Section 4
Looping Through a Nested List
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
Basic List Operations
CSE 160: Section 4
Functions that Modify Lists
CSE 160: Section 4
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
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
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
.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
List Slicing
(2 parameters)
listname[a : b] returns a new copy of the list in between indexes a and b.
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
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.
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
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
.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
.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
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
.reverse()
listname.reverse() reverses a list
Example:
Output:
lst = [1, 2, 3]
lst.reverse()
print(lst)
[3, 2, 1]
CSE 160: Section 4
.sort()
listname.sort() sorts a list
Example:
Output:
lst = [2, 1, 3]
lst.sort()
print(lst)
[1, 2, 3]
CSE 160: Section 4
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
Section Handout Problems
CSE 160: Section 4
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.
CSE 160: Section 4
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Written Check-In Due Friday
CSE 160: Section 4