python "for i in zip()"
时间: 2023-12-06 20:04:51 浏览: 35
The code snippet "for i in zip()" is used to iterate over two or more sequences at the same time by pairing up corresponding elements from each sequence. The zip() function takes one or more iterables as arguments and returns an iterator that produces tuples containing the corresponding elements from each iterable.
For example, consider the following code:
```
fruits = ['apple', 'banana', 'orange']
prices = [1.50, 0.99, 2.50]
for fruit, price in zip(fruits, prices):
print(fruit, price)
```
This will output:
```
apple 1.5
banana 0.99
orange 2.5
```
In this example, the `zip()` function is used to pair up corresponding elements from the `fruits` and `prices` lists, and the resulting iterator is used in the `for` loop to iterate over the pairs of elements. The loop body then unpacks each pair into the variables `fruit` and `price`, which are then used to print out the name of the fruit and its corresponding price.