# Python’s zip() Function

In Python, the zip() function is a powerful tool for combining iterables into tuples. This article aims to provide a comprehensive guide on its usage, applications, and significance through detailed examples.

Understanding zip() Function

The zip() function in Python is utilized to combine multiple iterables, such as lists, tuples, or strings, into tuples. Its syntax is straightforward:

zip(*iterables)


Here, iterables represent the sequences to be combined into tuples.

Example 1: Combining Lists

list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
zipped = zip(list1, list2)
print("Zipped result:", list(zipped))


Output 1:

Zipped result: [(1, 'a'), (2, 'b'), (3, 'c')]


Example 2: Combining Lists of Unequal Length

list1 = [1, 2, 3]
list2 = ['a', 'b']
zipped = zip(list1, list2)
print("Zipped result:", list(zipped))


Output 2:

Zipped result: [(1, 'a'), (2, 'b')]


Example 3: Combining Tuple and List

tuple1 = (10, 20, 30)
list1 = ['apple', 'banana', 'cherry']
zipped = zip(tuple1, list1)
print("Zipped result:", list(zipped))


Output 3:

Zipped result: [(10, 'apple'), (20, 'banana'), (30, 'cherry')]


Example 4: Using zip() with String

string1 = "hello"
string2 = "world"
zipped = zip(string1, string2)
print("Zipped result:", list(zipped))


Output 4:

Zipped result: [('h', 'w'), ('e', 'o'), ('l', 'r'), ('l', 'l'), ('o', 'd')]


Points to Remember

• The zip() function combines iterables into tuples.
• If the input iterables are of unequal length, the zipped result will be truncated to the shortest length.
• The resulting tuples contain elements from corresponding positions in the input iterables.
Author: user