The Zip() Function
The zip() function in Python is used to combine elements from two or more iterables (such as lists, tuples, or strings) based on their corresponding positions. It returns an iterator of tuples where the i-th tuple contains the i-th element from each of the input iterables.
Basic Syntax
zip(iterable1, iterable2, ...)
The resulting iterator can be converted into a list or unpacked in a loop to access the combined elements.
Example:
names = ["Alice", "Bob", "Charlie"]
ages = [25, 30, 35]
# Using zip to combine names and ages
combined = zip(names, ages)
# Converting the iterator to a list
result_list = list(combined)
print(result_list)
[('Alice', 25), ('Bob', 30), ('Charlie', 35)]