gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
Python zip
- User
Python zip
- Assistant
The
zipfunction in Python is used to merge multiple iterables into a single iterable of tuples. Each tuple contains one element from each of the input iterables, with the first tuple containing the first element from each iterable, the second tuple containing the second element from each iterable, and so on.Here is an example:
fruits = ["apple", "banana", "orange"] prices = [1.99, 0.99, 1.50] zip_obj = zip(fruits, prices) for fruit, price in zip_obj: print(f"The price of {fruit} is ${price:.2f}")Output:
The price of apple is $1.99 The price of banana is $0.99 The price of orange is $1.50In the above example, we use the
zipfunction to create an iterable of tuples containing each fruit and its corresponding price. We then use aforloop to iterate over each element of the iterable and print out the corresponding fruit and price.