From 55ed2af5939e064dc48d219cff41c4411e1e4dc4 Mon Sep 17 00:00:00 2001 From: PRATHVIRAJ Date: Sun, 27 Apr 2025 20:15:32 +0530 Subject: [PATCH 1/3] Update zip.md Adding other use cases and more examples for zip() built-in function --- docs/builtin/zip.md | 72 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 3 deletions(-) diff --git a/docs/builtin/zip.md b/docs/builtin/zip.md index e1c43446..fd9814ca 100644 --- a/docs/builtin/zip.md +++ b/docs/builtin/zip.md @@ -30,6 +30,72 @@ Python zip() built-in function # The shelf costs $40 ``` - - - +## Other Usecases + +The zip function in Python is used to combine two or more iterables (like lists or tuples) into a single iterable of tuples. + +```python +# Combining three lists +>>> list1 = [1, 2, 3] +>>> list2 = ['a', 'b', 'c'] +>>> list3 = [True, False, True] + +>>> zipped = zip(list1, list2, list3) +>>> print(list(zipped)) +# Output: [(1, 'a', True), (2, 'b', False), (3, 'c', True)] +``` + +### Unzipping + +```python + +# Unzipping a zipped object +>>> zipped = [(1, 'a'), (2, 'b'), (3, 'c')] +>>> list1, list2 = zip(*zipped) +>>> print(list1) +# Output: (1, 2, 3) +>>> print(list2) +# Output: ('a', 'b', 'c') +``` + +## More Examples + +### Zipping with Different Lengths + +When the iterables have different lengths, zip stops creating tuples when the shortest iterable is exhausted. + +```python +>>> numbers = [1, 2, 3] +>>> letters = ['a', 'b'] +>>> +>>> for num, letter in zip(numbers, letters): +... print(f'{num} -> {letter}') +# 1 -> a +# 2 -> b +``` + +### Using zip with Dictionaries + +You can use zip to combine keys and values from two lists into a dictionary. + +```python +>>> keys = ['name', 'age', 'city'] +>>> values = ['Alice', 25, 'New York'] +>>> +>>> my_dict = dict(zip(keys, values)) +>>> print(my_dict) +# {'name': 'Alice', 'age': 25, 'city': 'New York'} +``` + +### Using zip with List Comprehensions + +You can use zip in list comprehensions for more concise code. + +```python +>>> list1 = [1, 2, 3] +>>> list2 = [4, 5, 6] +>>> +>>> summed = [x + y for x, y in zip(list1, list2)] +>>> print(summed) +# [5, 7, 9] +``` From e4ec41006fe3bf11bf00f66fd3d0afda35c86220 Mon Sep 17 00:00:00 2001 From: PRATHVIRAJ Date: Sun, 27 Apr 2025 20:33:59 +0530 Subject: [PATCH 2/3] Update zip.md Changes to codacy checks --- docs/builtin/zip.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/builtin/zip.md b/docs/builtin/zip.md index fd9814ca..378fcc9e 100644 --- a/docs/builtin/zip.md +++ b/docs/builtin/zip.md @@ -32,7 +32,7 @@ Python zip() built-in function ## Other Usecases -The zip function in Python is used to combine two or more iterables (like lists or tuples) into a single iterable of tuples. +The zip function in Python combines multiple iterables into a single iterable of tuples. ```python # Combining three lists @@ -62,7 +62,7 @@ The zip function in Python is used to combine two or more iterables (like lists ### Zipping with Different Lengths -When the iterables have different lengths, zip stops creating tuples when the shortest iterable is exhausted. +zip stops creating tuples when the shortest iterable is exhausted. ```python >>> numbers = [1, 2, 3] From 790c27731cd9d28a385800cbabb8ad2d9ace78a5 Mon Sep 17 00:00:00 2001 From: PRATHVIRAJ Date: Sun, 27 Apr 2025 20:37:14 +0530 Subject: [PATCH 3/3] Update zip.md changes for codacy checks --- docs/builtin/zip.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/builtin/zip.md b/docs/builtin/zip.md index 378fcc9e..a55985c3 100644 --- a/docs/builtin/zip.md +++ b/docs/builtin/zip.md @@ -32,7 +32,7 @@ Python zip() built-in function ## Other Usecases -The zip function in Python combines multiple iterables into a single iterable of tuples. +The zip function in Python merges multiple iterables into tuples. ```python # Combining three lists