Exercise

Using and unpacking tuples

If you have a tuple like ('chocolate chip cookies', 15) and you want to access each part of the data, you can use an index just like a list. However, you can also "unpack" the tuple into multiple variables such as type, count = ('chocolate chip cookies', 15) that will set type to 'chocolate chip cookies' and count to 15.

Often you'll want to pair up multiple array data types. The zip() function does just that. It will return a list of tuples containing one element from each list passed into zip().

When looping over a list, you can also track your position in the list by using the enumerate() function. The function returns the index of the list item you are currently on in the list and the list item itself. (We'll talk more about the last line of code in our next lesson)

Instructions

100 XP
  • Use the zip() function to pair up girl_names and boy_names into a variable called pairs.
  • Use a for loop to loop through pairs, using enumerate() to keep track of your position. Unpack pairs into the variables rank and pair.
  • Unpack pair into the variables girl_name and boy_name.
  • Print the rank, girl name, and boy name, in that order. The rank is contained in rank.