From ea37cbb25457eaf0c650cd6822ff62fdb63da3a9 Mon Sep 17 00:00:00 2001 From: Lingamuneni Santhosh Siddhardha <103999924+Santhosh-Siddhardha@users.noreply.github.com> Date: Sun, 2 Jun 2024 22:07:01 +0530 Subject: [PATCH 1/5] Create list_comprehension.md Added Introduction Added Syntax Added Examples Added Conclusion --- contrib/advanced-python/list_comprehension.md | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 contrib/advanced-python/list_comprehension.md diff --git a/contrib/advanced-python/list_comprehension.md b/contrib/advanced-python/list_comprehension.md new file mode 100644 index 0000000..d9ab589 --- /dev/null +++ b/contrib/advanced-python/list_comprehension.md @@ -0,0 +1,73 @@ +# List Comprehension + +Creating lists concisely and expressively is what list comprehension in Python does. You can generate lists from already existing iterables like lists, tuples or strings with a short form. +This boosts the readability of code and reduces necessity of using explicit looping constructs. + +## Syntax : + +### Basic syntax + +```python +new_list = [expression for item in iterable] +``` +- **new_list**: This is the name given to the list that will be created using the list comprehension. +- **expression**: This is the expression that defines how each element of the new list will be generated or transformed. +- **item**: This variable represents each individual element from the iterable. It takes on the value of each element in the iterable during each iteration. +- **iterable**: This is the sequence-like object over which the iteration will take place. It provides the elements that will be processed by the expression. + +This list comprehension syntax `[expression for item in iterable]` allows you to generate a new list by applying a specific expression to each element in an iterable. + +### Syntax including condition + +```python +new_list = [expression for item in iterable if condition] +``` +- **new_list**: This is the name given to the list that will be created using the list comprehension. +- **expression**: This is the expression that defines how each element of the new list will be generated or transformed. +- **item**: This variable represents each individual element from the iterable. It takes on the value of each element in the iterable during each iteration. +- **iterable**: This is the sequence-like object over which the iteration will take place. It provides the elements that will be processed by the expression. +- **if condition**: This is an optional part of the syntax. It allows for conditional filtering of elements from the iterable. Only items that satisfy the condition + will be included in the new list. + + +## Examples: + +1. Generating a list of squares of numbers from 1 to 5: + +```python +squares = [x ** 2 for x in range(1, 6)] +print(squares) +``` + +- **Output** : +```python +[1, 4, 9, 16, 25] +``` + +2. Filtering even numbers from a list: + +```python +nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] +even = [x for x in nums if x % 2 == 0] +print(even) +``` + +- **Output** : +```python +[2, 4, 6, 8, 10] +``` + +3. Flattening a list of lists: +```python +matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] +flat = [x for sublist in matrix for x in sublist] +print(flat) +``` + +- **Output** : +```python +[1, 2, 3, 4, 5, 6, 7, 8, 9] +``` + +List comprehension is a powerful feature in Python for creating lists based on existing iterables with a concise syntax. +By mastering list comprehension, developers can write cleaner, more expressive code and leverage Python's functional programming capabilities effectively. From b1f8d147a2abd103e4029c4e366011c1831fe784 Mon Sep 17 00:00:00 2001 From: Lingamuneni Santhosh Siddhardha <103999924+Santhosh-Siddhardha@users.noreply.github.com> Date: Sun, 2 Jun 2024 22:08:52 +0530 Subject: [PATCH 2/5] Update index.md Added List Comprehension section --- contrib/advanced-python/index.md | 1 + 1 file changed, 1 insertion(+) diff --git a/contrib/advanced-python/index.md b/contrib/advanced-python/index.md index b31544e..7ae2ede 100644 --- a/contrib/advanced-python/index.md +++ b/contrib/advanced-python/index.md @@ -10,3 +10,4 @@ - [Protocols](protocols.md) - [Exception Handling in Python](exception-handling.md) - [Generators](generators.md) +- [List Comprehension](#list_comprehension.md) From 53efed98073fba08e3c2867676273f654882c9cb Mon Sep 17 00:00:00 2001 From: Lingamuneni Santhosh Siddhardha <103999924+Santhosh-Siddhardha@users.noreply.github.com> Date: Sun, 2 Jun 2024 22:11:33 +0530 Subject: [PATCH 3/5] Update index.md made a small change --- contrib/advanced-python/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/advanced-python/index.md b/contrib/advanced-python/index.md index 7ae2ede..631423a 100644 --- a/contrib/advanced-python/index.md +++ b/contrib/advanced-python/index.md @@ -10,4 +10,4 @@ - [Protocols](protocols.md) - [Exception Handling in Python](exception-handling.md) - [Generators](generators.md) -- [List Comprehension](#list_comprehension.md) +- [List Comprehension](list_comprehension.md) From f637a28ba1c2326adedeea7fdf633f48e2199e2f Mon Sep 17 00:00:00 2001 From: Ashita Prasad Date: Sat, 8 Jun 2024 11:00:46 +0530 Subject: [PATCH 4/5] Update index.md --- contrib/advanced-python/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contrib/advanced-python/index.md b/contrib/advanced-python/index.md index 631423a..f61fdf0 100644 --- a/contrib/advanced-python/index.md +++ b/contrib/advanced-python/index.md @@ -10,4 +10,4 @@ - [Protocols](protocols.md) - [Exception Handling in Python](exception-handling.md) - [Generators](generators.md) -- [List Comprehension](list_comprehension.md) +- [List Comprehension](list-comprehension.md) From c2740251604764cb12945841c828dc0033fbb151 Mon Sep 17 00:00:00 2001 From: Ashita Prasad Date: Sat, 8 Jun 2024 11:01:08 +0530 Subject: [PATCH 5/5] Rename list_comprehension.md to list-comprehension.md --- .../{list_comprehension.md => list-comprehension.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename contrib/advanced-python/{list_comprehension.md => list-comprehension.md} (100%) diff --git a/contrib/advanced-python/list_comprehension.md b/contrib/advanced-python/list-comprehension.md similarity index 100% rename from contrib/advanced-python/list_comprehension.md rename to contrib/advanced-python/list-comprehension.md