Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Recap
Tuples as return types in functions
Tuples as records
List of tuples
14. The zip() function – lists of different sizes
Summary

Instruction

Very well done! The zip() function also works fine when the lists are of different sizes. Take a look:

cars_dealer_a = [13000, 12300, 18900, 34500, 23900]
cars_dealer_b = [14000, 12000, 19000]

for car in zip(cars_dealer_a, cars_dealer_b):
  if car[0] < car[1]:
    print('Dealer A is cheaper by', car[1] - car[0])
  elif car[1] < car[0]:
    print('Dealer B is cheaper by', car[0] - car[1])
  else:
    print('Equal offers')

In the example above, cars_dealer_a now contains five elements, while cars_dealer_b has three elements; still, the code works in the same way. This is because zip() automatically finds the list with the smaller number of elements, in other words, it stops when a zip element (teeth) is missing. In the example above, the additional two elements in cars_dealer_a are simply ignored.

If for some reason you want to iterate over the longer list, you need to use another method – zip() won't work in that case.

Exercise

You are given the 2017 and 2018 job titles for three employees (in the same order). Also, at the end of the positions_2018, there are some additional job titles (as there are some new employees in 2018). How many employees changed their job titles? Use the zip() function to find out and print:

In 2018, {x} people changed their job titles!

Stuck? Here's a hint!

Create a temporary counter before the for loop. Inside the loop, increase the counter when the old job title does not equal the new job title.