Get pairs of values from parallel lists in Python using the zip() function
Coding with Python
🕑 This lesson will take about 5 minutes
Python’s zip() function can be used to get pairs of values from two lists, for example, the elements in index position [0] from two different lists, the elements in index position [1] from two different lists, etc. Check out the example code below, where there are two lists containing different words in English and Spanish translations. The equivalent words are in the same order in each list.
The code above would produce the following output:
Hello in Spanish is Hola
Goodbye in Spanish is Adios
Thank you in Spanish is Gracias
The zip() function takes pairs of values from each list and returns them as tuples, like in the example below: