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
13. Comparing two lists of different sizes
Congratulations

## Instruction

Perfect! What happens when we want to compare elements in two lists of different lengths? In such cases, we need to think carefully about the indexes of both lists. Python can throw an IndexError if we try to access an element that is beyond the bounds of a list.

mark_sleep_durations = [368, 690, 426, 494, 445, 690, 423, 534, 606, 390]
kate_sleep_durations = [546, 562, 474, 440, 299]
mark_longer_count = 0 # mark_longer_count stores a number of times when Mark slept longer.
for i in range(min(len(mark_sleep_durations), len(kate_sleep_durations))):
if mark_sleep_durations[i] > kate_sleep_durations[i]:
mark_longer_count += 1
print(mark_longer_count)


One way of dealing with lists of different lengths is by iterating over the indexes of the shorter list. Note that we changed

for i in range(len(mark_sleep_durations)):
to
for i in range(min(len(mark_sleep_durations), len(kate_sleep_durations))):

We will only iterate as many times as there are elements in a shorter list. In this way, we prevent IndexErrors.

## Exercise

In 2017, Martha had two credit cards: one that she used during the whole year, and another that she only used for the first six months. In the template, you have the monthly spending values from both cards. Your task is to create a new list containing total monthly spending for the entirety of 2017. Round the sums to integers and print the list.

### Stuck? Here's a hint!

First, use an if statement to find out which list is longer. Store the length of the longer list inside a temporary variable. Next, use that temporary variable in a for loop with a temporary i variable for the index value.

Inside the loop, compare i to the length of the first and second lists and create a monthly_total variable accordingly.