Python Program - Iterate Through Two Lists in Parallel
www.iditfigea.com
To iterate through two lists in parallel in Python, you can use the built-in zip()
function. Here's an example program:
fruits = ["apple", "banana", "cherry"] quantities = [1, 2, 3] for fruit, quantity in zip(fruits, quantities): print("Fruit:", fruit, "Quantity:", quantity)
In this program, the zip()
function is used to loop over both the fruits
and quantities
lists simultaneously. The fruit
variable will contain the current item from the fruits
list being processed, and the quantity
variable will contain the corresponding item from the quantities
list. The print()
function is then used to display the items from both lists.
When you run this program, the output will be:
Fruit: apple Quantity: 1 Fruit: banana Quantity: 2 Fruit: cherry Quantity: 3