From a3961ff390a95c24b33bca10b047f89ec63a7201 Mon Sep 17 00:00:00 2001
From: Pradnya Gaitonde <116059908+PradnyaGaitonde@users.noreply.github.com>
Date: Wed, 5 Jun 2024 00:44:22 +0530
Subject: [PATCH 1/2] Create Itertools_module.md
Added content for itertools module in python.
---
contrib/advanced-python/Itertools_module.md | 144 ++++++++++++++++++++
1 file changed, 144 insertions(+)
create mode 100644 contrib/advanced-python/Itertools_module.md
diff --git a/contrib/advanced-python/Itertools_module.md b/contrib/advanced-python/Itertools_module.md
new file mode 100644
index 0000000..501a127
--- /dev/null
+++ b/contrib/advanced-python/Itertools_module.md
@@ -0,0 +1,144 @@
+# The 'itertools' Module in Python
+The itertools module in Python provides a collection of fast, memory-efficient tools that are useful for creating and working with iterators. These functions
+allow you to iterate over data in various ways, often combining, filtering, or extending iterators to generate complex sequences efficiently.
+
+## Benefits of itertools
+1. Efficiency: Functions in itertools are designed to be memory-efficient, often generating elements on the fly and avoiding the need to store large intermediate results.
+2. Conciseness: Using itertools can lead to more readable and concise code, reducing the need for complex loops and temporary variables.
+3. Composability: Functions from itertools can be easily combined, allowing you to build complex iterator pipelines from simple building blocks.
+
+## Useful Functions in itertools
+Here are some of the most useful functions in the itertools module, along with examples of how to use them:
+
+1. 'count': Generates an infinite sequence of numbers, starting from a specified value.
+
+```bash
+import itertools
+
+counter = itertools.count(start=10, step=2)
+for _ in range(5):
+ print(next(counter))
+# Output: 10, 12, 14, 16, 18
+```
+
+2. 'cycle': Cycles through an iterable indefinitely.
+
+```bash
+import itertools
+
+cycler = itertools.cycle(['A', 'B', 'C'])
+for _ in range(6):
+ print(next(cycler))
+# Output: A, B, C, A, B, C
+```
+
+3.'repeat': Repeats an object a specified number of times or indefinitely.
+
+```bash
+import itertools
+
+repeater = itertools.repeat('Hello', 3)
+for item in repeater:
+ print(item)
+# Output: Hello, Hello, Hello
+```
+
+4. 'chain': Combines multiple iterables into a single iterable.
+
+```bash
+import itertools
+
+combined = itertools.chain([1, 2, 3], ['a', 'b', 'c'])
+for item in combined:
+ print(item)
+# Output: 1, 2, 3, a, b, c
+```
+
+5. 'islice': Slices an iterator, similar to slicing a list.
+
+```bash
+import itertools
+
+sliced = itertools.islice(range(10), 2, 8, 2)
+for item in sliced:
+ print(item)
+# Output: 2, 4, 6
+```
+
+6. 'compress': Filters elements in an iterable based on a corresponding selector iterable.
+
+```bash
+import itertools
+
+data = ['A', 'B', 'C', 'D']
+selectors = [1, 0, 1, 0]
+result = itertools.compress(data, selectors)
+for item in result:
+ print(item)
+# Output: A, C
+```
+
+7. 'permutations': Generates all possible permutations of an iterable.
+
+```bash
+import itertools
+
+perms = itertools.permutations('ABC', 2)
+for item in perms:
+ print(item)
+# Output: ('A', 'B'), ('A', 'C'), ('B', 'A'), ('B', 'C'), ('C', 'A'), ('C', 'B')
+```
+
+8. 'combinations': Generates all possible combinations of a specified length from an iterable.
+
+```bash
+import itertools
+
+combs = itertools.combinations('ABC', 2)
+for item in combs:
+ print(item)
+# Output: ('A', 'B'), ('A', 'C'), ('B', 'C')
+```
+
+9. 'product': Computes the Cartesian product of input iterables.
+
+```bash
+import itertools
+
+prod = itertools.product('AB', '12')
+for item in prod:
+ print(item)
+# Output: ('A', '1'), ('A', '2'), ('B', '1'), ('B', '2')
+```
+
+10. 'groupby': Groups elements of an iterable by a specified key function.
+
+```bash
+import itertools
+
+data = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 25}, {'name': 'Charlie', 'age': 30}]
+sorted_data = sorted(data, key=lambda x: x['age'])
+grouped = itertools.groupby(sorted_data, key=lambda x: x['age'])
+for key, group in grouped:
+ print(key, list(group))
+# Output:
+# 25 [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 25}]
+# 30 [{'name': 'Charlie', 'age': 30}]
+```
+
+11. 'accumulate': Makes an iterator that returns accumulated sums, or accumulated results of other binary functions specified via the optional func argument.
+
+```bash
+import itertools
+import operator
+
+data = [1, 2, 3, 4, 5]
+acc = itertools.accumulate(data, operator.mul)
+for item in acc:
+ print(item)
+# Output: 1, 2, 6, 24, 120
+```
+
+## Conclusion
+The itertools module is a powerful toolkit for working with iterators in Python. Its functions enable efficient and concise handling of iterable data, allowing you to create complex data processing pipelines with minimal memory overhead.
+By leveraging itertools, you can improve the readability and performance of your code, making it a valuable addition to your Python programming arsenal.
From 3e346a29a6bcddd25fe0c7af7b8b2dbffe74a3da Mon Sep 17 00:00:00 2001
From: Ashita Prasad
Date: Sat, 8 Jun 2024 11:37:03 +0530
Subject: [PATCH 2/2] Rename Itertools_module.md to itertools.md
---
contrib/advanced-python/{Itertools_module.md => itertools.md} | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename contrib/advanced-python/{Itertools_module.md => itertools.md} (100%)
diff --git a/contrib/advanced-python/Itertools_module.md b/contrib/advanced-python/itertools.md
similarity index 100%
rename from contrib/advanced-python/Itertools_module.md
rename to contrib/advanced-python/itertools.md