the Zip() Function
The zip()
function can be useful when working with multiple lists or other iterable objects and you want to iterate over their elements in pairs, or when you want to combine multiple iterable objects into one.
## Example of using the zip() function in a for loop in Python
## Sample lists to iterate over
fruits = ['apple', 'banana', 'cherry']
prices = [1.2, 3.5, 2.5]
## Using a for loop and the zip function to iterate over the lists
for fruit, price in zip(fruits, prices):
print(f"Fruit: {fruit}, Price: ${price}")
## Output:
## Fruit: apple, Price: $1.2
## Fruit: banana, Price: $3.5
## Fruit: cherry, Price: $2.5
In the above example, we define two lists fruits
and prices
containing the elements apple
, banana
, cherry
and 1.2
, 3.5
, 2.5
respectively.
Then, we use a for loop to iterate over the elements from the two lists using the zip()
function. The zip()
function takes multiple iterable objects as input and returns an iterator that generates tuples containing the elements of the input iterables at corresponding positions. In the example above, we use the unpacking operator (fruit
, price
) to assign the elements of each tuple to the variables fruit
and price
, respectively. Inside the loop, we use string formatting to print out the Fruit and the price.
If the length of the input iterable is not equal, the zip()
function will stop at the end of the shortest iterable.