From cdef263e594dee1eb4b7a27d5f3e92095e861626 Mon Sep 17 00:00:00 2001 From: myckellr <51352774+myckellr@users.noreply.github.com> Date: Fri, 26 Nov 2021 16:57:27 -0500 Subject: [PATCH 1/6] Create list-comprehension.md Adding additional .md file for list comprehension --- basics/list-comprehension.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 basics/list-comprehension.md diff --git a/basics/list-comprehension.md b/basics/list-comprehension.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/basics/list-comprehension.md @@ -0,0 +1 @@ + From 09b2ef17e6cf9add5c931bcc5084434da1255725 Mon Sep 17 00:00:00 2001 From: myckellr <51352774+myckellr@users.noreply.github.com> Date: Fri, 26 Nov 2021 16:58:21 -0500 Subject: [PATCH 2/6] Update README.md --- basics/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/basics/README.md b/basics/README.md index 24f8547..c5e365f 100644 --- a/basics/README.md +++ b/basics/README.md @@ -27,6 +27,7 @@ to learn more about whatever you want after studying it. 19. [Exceptions](exceptions.md) 20. [Classes](classes.md) 21. [Docstrings](docstrings.md) +22. [List Comprehension](list-comprehension.md) *** From 70274ddac63bc57771e95083d07aae1a07fb0f94 Mon Sep 17 00:00:00 2001 From: myckellr <51352774+myckellr@users.noreply.github.com> Date: Fri, 26 Nov 2021 17:34:02 -0500 Subject: [PATCH 3/6] Update list-comprehension.md Added information to list-comprehension.md --- basics/list-comprehension.md | 54 ++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/basics/list-comprehension.md b/basics/list-comprehension.md index 8b13789..9b4b86f 100644 --- a/basics/list-comprehension.md +++ b/basics/list-comprehension.md @@ -1 +1,55 @@ +# List Comprehension + + +## Definition +List comprehensions creates a new list from an existing list that is a shorter syntax than normal. It also processes the list much faster than using a for loop. + + +## Without List Comprehension +```python +animals = ["dog", "cat", "giraffe", "donkey", "ape"] +arr = [] + +for i in animals: + if len(i) == 3: + arr.append(i) + +print(arr) +``` +```python +['dog', 'cat', 'ape'] +``` +The example above shows a list of animals and the for loop iterates through the list of animals and the if statement filters the list and only adds the animal if their name is exactly 3 characters long. + +## With List Comprehension +``` +animals = ["dog", "cat", "giraffe", "donkey", "ape"] + +arr = [i for i in animals if len(i) == 3] +print(arr) +``` +```python +['dog', 'cat', 'ape'] +``` +The example above does exactly the same thing but is in a much more concise syntax where it iterates through the list of animals and appends to the new list if their name is exactly 3 characters long. + +## Syntax + +newlist = [**expression** for **item** in **iterable** if **condition**] +This is the usual form in which list comprehension is used. + +**Expression:** It is the current item for each iteration but also serves as the outcome which can be manipulated before it becomes added to the new list. +```python +animals = ["DOG", "CAT", "GIRAFFE", "DONKEY", "APE"] + +arr = [i.lower() for i in animals if len(i) == 3] +print(arr) +``` +```python +['dog', 'cat', 'ape'] +``` +The **i** is essentially an animal each iteration where it checks if the animal's name is 3 characters long and if it does it **also** changes that animal name to all lower case before it finally appends or adds it to the list. + + + From 2258e97fc66fa9a304a0a1dd05a14acbe32ee718 Mon Sep 17 00:00:00 2001 From: myckellr <51352774+myckellr@users.noreply.github.com> Date: Sun, 28 Nov 2021 01:27:36 -0500 Subject: [PATCH 4/6] Update list-comprehension.md --- basics/list-comprehension.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basics/list-comprehension.md b/basics/list-comprehension.md index 9b4b86f..1ef1113 100644 --- a/basics/list-comprehension.md +++ b/basics/list-comprehension.md @@ -22,7 +22,7 @@ print(arr) The example above shows a list of animals and the for loop iterates through the list of animals and the if statement filters the list and only adds the animal if their name is exactly 3 characters long. ## With List Comprehension -``` +```python animals = ["dog", "cat", "giraffe", "donkey", "ape"] arr = [i for i in animals if len(i) == 3] From 1cc24786fd818096d7d97a7ec7cdd5a22ef73550 Mon Sep 17 00:00:00 2001 From: myckellr <51352774+myckellr@users.noreply.github.com> Date: Thu, 2 Dec 2021 19:21:34 -0500 Subject: [PATCH 5/6] Update list-comprehension.md --- basics/list-comprehension.md | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/basics/list-comprehension.md b/basics/list-comprehension.md index 1ef1113..799e215 100644 --- a/basics/list-comprehension.md +++ b/basics/list-comprehension.md @@ -48,8 +48,30 @@ print(arr) ```python ['dog', 'cat', 'ape'] ``` -The **i** is essentially an animal each iteration where it checks if the animal's name is 3 characters long and if it does it **also** changes that animal name to all lower case before it finally appends or adds it to the list. - +The **i** is essentially the **item** which is an animal in each iteration where it checks if the animal's name is 3 characters long and if it does it **also** changes that animal name to all lower case before it finally appends or adds it to the list. +**Iterable:** The iterable is the object that is being iterated over and can be any iterable object such as a set, list, tuple etc. +```python +arr = [i for i in range(5)] +print(arr) +``` +```python +[0, 1, 2, 3, 4] +``` +This essentially adds 0 to 4 to the new array with range(5). +**Condition** The condition is a sort of filter that is specified to get the specific data that is wanted. +```python +arr = [i for i in range(10) if i < 5] +print(arr) +``` +```python +[0, 1, 2, 3, 4] +``` +For this example the condition is that only the numbers that is less than 5 would get put into the array. +##Summary +- List comprehensions are a good way to shorten code +- They are versatile when it comes to iterating through iterable datasets +- Not only can you filter out data with a condition you can also change the data before it gets added to the new list +- List comprehensions normally follow: newlist = [**expression** for **item** in **iterable** if **condition**] From 3cb058e2cd0dfe4c850f9a94f36a845c57bf270e Mon Sep 17 00:00:00 2001 From: myckellr <51352774+myckellr@users.noreply.github.com> Date: Thu, 2 Dec 2021 19:22:28 -0500 Subject: [PATCH 6/6] Update list-comprehension.md --- basics/list-comprehension.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/basics/list-comprehension.md b/basics/list-comprehension.md index 799e215..ac5387b 100644 --- a/basics/list-comprehension.md +++ b/basics/list-comprehension.md @@ -70,7 +70,7 @@ print(arr) ``` For this example the condition is that only the numbers that is less than 5 would get put into the array. -##Summary +## Summary - List comprehensions are a good way to shorten code - They are versatile when it comes to iterating through iterable datasets - Not only can you filter out data with a condition you can also change the data before it gets added to the new list