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]
).