Looping through lists with zip #
We can loop through multiple sequences, such as strings, lists, or tuples with zip().
Here's what happens when you call the zip() function with multiple lists:
list_a = [1,2,3]
list_b = [4,5,6]
print(list(zip(list_a, list_b)))
# [(1, 4), (2, 5), (3, 6)]
From the example above, we call the function zip() with two lists. The function then returns us a list of tuples, each tuple contains two elements from each list.
With this knowledge, we can now loop through multiple lists with for loop, zip(), and data unpacking:
names = ['John', 'Alice', 'Bob']
ages = [30, 25, 35]
for name, age in zip(names, ages):
print(f"{name} is {age} years old")
Output:
John is 30 years old
Alice is 25 years old
Bob is 35 years old
We can also loop more than 3 lists at the same time as well:
names = ['John', 'Alice', 'Bob']
ages = [30, 25, 35]
countries = ['USA', 'Canada', 'UK']
combined = zip(names, ages, countries)
for name, age, country in combined:
print(name, age, country)
Exercise #
Combine the values in the lists students and scores into a new list results.
Tests #
- Variable
resultsshould be['Rhonda Hart: 60', 'Ernest Aguirre: 70', 'Sean Jackson: 80'] - Use
zip()function
Hints #
Each element in results should be a string in the format "{student}: {score}". For example, "Rhonda Hart: 60".
Getting Started with Python
Data Types
Python Functions
Statements in Python
Basic Debugging in Python
Basic Algorithm
Object-Oriented Programming
Error Handling
Intermediate Algorithm
Python Modules