The `zip` function in Python allows you to combine multiple iterables (like lists, tuples, etc.) into a single iterable of tuples. This can be particularly useful in data science for handling two-dimensional data, among other applications. In this tutorial, we'll explore how to use the `zip` function through various examples. ### Basic Usage of the `zip` Function Let's start with a simple example to see how the `zip` function works.
# Define two lists list1 = [1, 2, 3] list2 = ['a', 'b', 'c'] # Combine using zip zipped = zip(list1, list2) # Convert to a list of tuples zipped_list = list(zipped) print(zipped_list)
In this example, we have two lists, `list1` and `list2`. The `zip` function combines these lists into pairs, creating a new iterable of tuples. ### Iterating Over Zipped Objects You can iterate over zipped objects directly in a loop. Here's how:
# Define two lists
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
# Combine using zip and iterate
for number, letter in zip(list1, list2):
print(f'Number: {number}, Letter: {letter}')In this snippet, we zip `list1` and `list2` and then iterate over the combined pairs, printing each pair's elements. ### Using `zip` with Lists of Different Lengths If the lists passed into `zip` have different lengths, the resulting list of tuples will be truncated to the length of the shortest input iterable.
# Define lists of different lengths list1 = [1, 2, 3, 4] list2 = ['a', 'b'] # Combine using zip zipped_list = list(zip(list1, list2)) print(zipped_list)
Here, `list1` is longer than `list2`, so the zipped result only includes pairs up to the length of the shorter list. ### Unzipping Using `zip` The `zip` function can also be used to unzip a list of tuples back into individual lists.
# Define a list of tuples zipped_list = [(1, 'a'), (2, 'b'), (3, 'c')] # Unzip the list numbers, letters = zip(*zipped_list) print(numbers) print(letters)
In this example, `zip(*zipped_list)` effectively reverses the zipping process, separating the tuple elements back into individual lists. ### Using `zip` with More Than Two Iterables You can zip together more than two iterables if needed.
# Define three lists list1 = [1, 2, 3] list2 = ['a', 'b', 'c'] list3 = [10.1, 20.2, 30.3] # Combine using zip zipped_list = list(zip(list1, list2, list3)) print(zipped_list)
This code snippet demonstrates zipping three lists together into a single list of tuples. ### Using `zip` with Dictionaries You can also use `zip` with [dictionaries](/tutorials/dict). When you zip dictionaries, it normally zips their keys.
# Define two dictionaries
dict1 = {'name': 'Alice', 'age': 25}
dict2 = {'name': 'Bob', 'age': 30}
# Combine using zip
zipped_dict = list(zip(dict1, dict2))
print(zipped_dict)
# To zip dictionary values
zipped_dict_values = list(zip(dict1.values(), dict2.values()))
print(zipped_dict_values)In this example, we demonstrate zipping dictionary keys and values. ### Using `zip` with List Comprehensions for Transformation You can use `zip` with [list comprehensions](/tutorials/list-comprehension) to perform transformations or computations on the pairs.
# Define two lists list1 = [1, 2, 3] list2 = [4, 5, 6] # Compute the sum of pairs using list comprehension sum_list = [x + y for x, y in zip(list1, list2)] print(sum_list)
In this code snippet, we compute the sum of corresponding elements from two lists using `zip` and a list comprehension. ### Conclusion The `zip` function can simplify a range of tasks involving lists and other iterables and help you write more concise and readable code for combining data or splitting it apart.