Q2

More loops, this time with generators.

A

Print out the corresponding elements of the three lists, list1, list2, and list3, simultaneously using a loop e.g. list1[0], list2[0], and list3[0].


In [ ]:
import numpy as np
np.random.seed(85473)

list1 = np.random.randint(100, size = 10).tolist()
list2 = np.random.randint(100, size = 10).tolist()
list3 = np.random.randint(100, size = 10).tolist()

### BEGIN SOLUTION

### END SOLUTION

B

Print out the indices where the corresponding elements of all four lists--list1, list2, list3, and list4--are exactly the same.


In [ ]:
import numpy as np
np.random.seed(5894292)

list1 = np.random.randint(3, size = 100).tolist()
list2 = np.random.randint(3, size = 100).tolist()
list3 = np.random.randint(3, size = 100).tolist()
list4 = np.random.randint(3, size = 100).tolist()

### BEGIN SOLUTION

### END SOLUTION

C

Write a loop that iterates through both list1 and list2 simultaneously, and which halts immediately once the sum of the two corresponding elements of the lists is greater than 15.


In [ ]:
import numpy as np
np.random.seed(447388)

list1 = np.random.randint(10, size = 10).tolist()
list2 = np.random.randint(10, size = 10).tolist()

### BEGIN SOLUTION

### END SOLUTION

D

Create a new dictionary where the keys and values of original_dictionary are flipped.


In [ ]:
original_dictionary = {1:2, 3:4, 5:6, 7:8, 9:10}
print(original_dictionary)

new_dictionary = {}

### BEGIN SOLUTION

### END SOLUTION