CSE 160 Section 5
Lists!
CSE 160
CSE 160: Section 5
Logistics
CSE 160: Section 5
Lecture Review: Lists
CSE 160: Section 5
Lists
lst = [10, 20, 30]
# 0 1 2 <- index
lst[0] # 10
len(lst) # 3
CSE 160: Section 5
Lists
# Making a list
lst = [item0, item1, item2]
# Using a list
lst[i]
len(lst)
lst[i] = x
lst.append(x)
lst[:]
for i in range(len(lst)):
Syntax | Description |
lst[i] | Get the element at index i |
len(lst) | Return the number of elements in lst |
lst[i] = x | Change the element at index i to x |
lst.append(x) | Add x to the end of the list |
lst[:] | Make a copy of the list |
for i in range(len(lst)): | Loop through every list index |
CSE 160: Section 5
Section Handout Problems
CSE 160: Section 5
Warm-Up Problem
The function below uses a list of student heights (student_lst) and returns the max height in inches. There are 2 bugs in the program, each on different lines. Annotate (write on) the code below to fix the bugs so that the program behaves as expected. You may add, delete or replace code.
student_lst = [72, 63, 52, 70, 68]
def max_height(student_lst):
tallest = student_lst[0]
for student in len(student_lst):
height = student_lst[student]
if height > tallest:
return tallest
CSE 160: Section 5
Warm-Up Problem
The function below uses a list of student heights (student_lst) and returns the max height in inches. There are 2 bugs in the program, each on different lines. Annotate (write on) the code below to fix the bugs so that the program behaves as expected.
student_lst = [72, 63, 52, 70, 68]
def max_height(student_lst):
tallest = student_lst[0]
for student in range(len(student_lst)):
height = student_lst[student]
if height > tallest:
tallest = height
return tallest
CSE 160: Section 5
Problem 1: List Printing
What values would be printed when you run the following lines of code?
list_1 = [1, 2, 3, 4, 5]
list_2 = list_1
list_3 = list_1[:] # equivalent to list_1[0:5]
list_2[0] = 98
list_1[4] = 99
print("List 1:", list_1)
print("List 2:", list_2)
print("List 3:", list_3)
CSE 160: Section 5
Problem 1: List Printing
list_1 = [1, 2, 3, 4, 5]
list_2 = list_1
list_3 = list_1[:] # equivalent to list_1[0:5]
list_2[0] = 98
list_1[4] = 99
print("List 1:", list_1)
print("List 2:", list_2)
print("List 3:", list_3)
List 1: [98, 2, 3, 4, 99]
List 2: [98, 2, 3, 4, 99]
List 3: [1, 2, 3, 4, 5]
CSE 160: Section 5
Problem 2: Lists and Functions!
Write a function called count(lst, value) that takes in a list and a value. It should return the number of times the value appears in the list.
my_lst = [1, 3, 5, 3, 2, 5, 6, 3, 3]
count(my_lst, 3) should return 4.
CSE 160: Section 5
Problem 2: Lists and Functions!
Write a function called count(lst, value) that takes in a list and a value. It should return the number of times the value appears in the list.
my_lst = [1, 3, 5, 3, 2, 5, 6, 3, 3]
count(my_lst, 3) should return 4.
def count(lst, value):
total = 0
for item in lst:
if item == value:
total += 1
return total
CSE 160: Section 5
Problem 3: Calculating dot product
Create a function dot_product(list1, list2) which takes in two lists of integers and returns their dot product. Assume the lists are of equal length and contain only integer values.
The dot product of two lists of numbers is the sum of the products of each pair of numbers in the two lists at the same index.
For example, the dot product [1, 2] and [3, 4] is: 1 * 3 + 2 * 4 = 11.
CSE 160: Section 5
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 5
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 5
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 5
Problem 3: Calculating dot product
Create a function dot_product(list1, list2) which takes in two lists of integers and returns their dot product. Assume the lists are of equal length and contain only integer values. The dot product [1, 2] and [3, 4] is: 1 * 3 + 2 * 4 = 11.
def dot_product(list1, list2):
total = 0
for i in range(len(list1)):
total += list1[i] * list2[i]
return total
CSE 160: Section 5
Exit Ticket
Write a function reverse_list that returns a new list containing the elements of my_list in reverse order.
For example:
my_list = ["Cici", "Daniel", "Vaishi", "Kellen"]
reverse_list(my_list) should return ["Kellen", "Vaishi", "Daniel", "Cici"]
CSE 160: Section 5
Exit Ticket
Write a function reverse_list that returns a new list containing the elements of my_list in reverse order.
For example, my_list = ["Cici", "Daniel", "Vaishi", "Kellen"]
reverse_list(my_list) should return ["Kellen", "Vaishi", "Daniel", "Cici"]
def reverse_list(my_list):
result = []
for i in range(len(my_list) - 1, -1, -1):
result.append(my_list[i])
return result
CSE 160: Section 5
Problem 4: Am I in your Best Friends list?
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"]
Given a name and a friend list, write a function that returns 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.
CSE 160: Section 5
Problem 4: Am I in your Best Friends list?
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"]
def find_index(name, friend_list):
for i in range(len(friend_list)):
if friend_list[i] == name:
return i
return -1
CSE 160: Section 5
Problem 5: Positive Numbers Only
Write a function positive_numbers(lst) that returns a new list containing only the positive numbers from lst.
Note: A number is positive if it is strictly greater than zero.
For example:
positive_numbers ([3, -2, 0, 5, -1]) should return [3, 5]
positive_numbers ([-4, -7, -2]) should return [ ]
CSE 160: Section 5
Problem 5: Positive Numbers Only
Write a function positive_numbers(lst) that returns a new list containing only the positive numbers from lst. Note: A number is positive if it is strictly greater than zero.
positive_numbers ([3, -2, 0, 5, -1]) should return [3, 5]
positive_numbers ([-4, -7, -2]) should return [ ]
def positive_numbers(lst):
result = []
for num in lst:
if num > 0:
result.append(num)
return result
CSE 160: Section 5
Thanks for Coming!
CSE 160: Section 5