Again, let’s return to our class height example:
- Jenny is 61 inches tall
- Alexus is 70 inches tall
- Sam is 67 inches tall
- Grace is 64 inches tall
Suppose that we already had a list of names and a list of heights:
names = ['Jenny', 'Alexus', 'Sam', 'Grace'] heights = [61, 70, 67, 65]
If we wanted to create a list of lists that paired each name with a height, we could use the command zip
. zip
takes two (or more) lists as inputs and returns an object that contains a list of pairs. Each pair contains one element from each of the inputs. You won’t be able to see much about this object from just printing it:
names_and_heights = zip(names, heights) print(names_and_heights)
because it will return the location of this object in memory. Output would look something like this:
<zip object at 0x7f1631e86b48>
To see the nested lists, you can convert the zip object to a list
first:
print(list(names_and_heights))
returns:
[('Jenny', 61), ('Alexus', 70), ('Sam', 67), ('Grace', 65)]
Instructions
Use zip
to create a new variable called names_and_dogs_names
that combines names
and dogs_names
into a zip
object.
Make sure to run the code for this step before proceeding to the next instruction!
Create a new variable named list_of_names_and_dogs_names
by calling list()
on names_and_dogs_names
. Print this new variable .