Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Recap
Iterating over a list
Modifying lists
Working with multiple lists
12. Comparing two lists of the same size
Congratulations

Instruction

Well done! Another frequent operation is comparing elements in two lists of the same size.

Let's assume we have two people, Mark and Kate, who tracked their sleep durations on the same nights. We now want to compare those periods and find out how many times Mark slept longer than Kate. Take a look:

mark_sleep_durations = [368, 690, 426, 494, 445, 690, 423, 534, 606, 390]
kate_sleep_durations = [546, 562, 474, 440, 299, 561, 399, 330, 547, 471]
mark_longer_count = 0
for i in range(len(mark_sleep_durations)):
  if mark_sleep_durations[i] > kate_sleep_durations[i]:
    mark_longer_count += 1
print(mark_longer_count)

This is a classic solution, typically available in most programming languages. We use a for loop to generate all indexes found in the first list. Inside the loop, we use the current i value to access elements in both lists (mark_sleep_durations[i] and kate_sleep_durations[i]).

Exercise

Martha has a husband named Tim. You have both Martha's and Tim's monthly expenditures. Create a new list named household_monthly_spendings, which will contain the total monthly expenses (defined as the sum of Martha's and Tim's expenses). Round values to the nearest integer. Print the household_monthly_spendings list.

Stuck? Here's a hint!

Use round(x) to round x to the nearest integer.