From 01a525d1a1f5cd38f59179acfe20348f2523f5bd Mon Sep 17 00:00:00 2001 From: Afnan Sabir <133896404+sabir-10@users.noreply.github.com> Date: Thu, 30 Jul 2026 13:37:42 -0700 Subject: [PATCH] Update README.md Added Iteration tools --- README.md | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/README.md b/README.md index b6f6180..1504759 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,8 @@ A Cheat Sheet 📜 to **revise** Python syntax in **less time**. Particularly us - [Sets](#sets) - [Tuples](#tuples) - [Strings](#strings) +- [Collections & Iteration Tools](#collections--iteration-tools) + - [Itertools](#itertools) - [Built-in Functions](#built-in-functions) - [Advanced Topics](#advanced-topics) - [Best Practices](#best-practices) @@ -199,7 +201,58 @@ chr(97) # ASCII to char ('a') # Join Lists ''.join(['a','b']) # Concatenate list elements ``` +## Collections & Iteration Tools +### Itertools + +```python +from itertools import ( + combinations, + permutations, + product, + accumulate, + groupby, +) +``` + +#### Combinations + +```python +list(combinations([1, 2, 3], 2)) +# [(1, 2), (1, 3), (2, 3)] +``` + +#### Permutations + +```python +list(permutations([1, 2, 3], 2)) +# [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)] +``` + +#### Product + +```python +list(product([1, 2], repeat=2)) +# [(1, 1), (1, 2), (2, 1), (2, 2)] +``` + +#### Accumulate + +```python +list(accumulate([1, 2, 3, 4])) +# [1, 3, 6, 10] +``` + +#### Groupby + +```python +for key, group in groupby("aaabbc"): + print(key, list(group)) + +# a ['a', 'a', 'a'] +# b ['b', 'b'] +# c ['c'] +``` # Built-in Functions ```python