| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,77 @@ | ||||||||||||
| # List Comprehension | ||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| ## Definition | ||||||||||||
|
Comment thread
Copy link
Copy Markdown
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityThis doesn't have to be under a second title.
Suggested change
Sorry, something went wrong.
All reactions
|
||||||||||||
| 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. | ||||||||||||
|
Comment thread
Copy link
Copy Markdown
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
Sorry, something went wrong.
All reactions
|
||||||||||||
|
|
||||||||||||
|
|
||||||||||||
| ## Without List Comprehension | ||||||||||||
| ```python | ||||||||||||
| animals = ["dog", "cat", "giraffe", "donkey", "ape"] | ||||||||||||
| arr = [] | ||||||||||||
|
|
||||||||||||
| for i in animals: | ||||||||||||
|
Comment thread
Comment on lines
+11
to
+13
Copy link
Copy Markdown
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityConsider improving the variable names, e.g. arr --> filtered_animals, i --> animal, also in the list comprehension below.
Sorry, something went wrong.
All reactions
|
||||||||||||
| if len(i) == 3: | ||||||||||||
| arr.append(i) | ||||||||||||
|
|
||||||||||||
| print(arr) | ||||||||||||
| ``` | ||||||||||||
| ```python | ||||||||||||
| ['dog', 'cat', 'ape'] | ||||||||||||
|
Comment thread
Comment on lines
+17
to
+20
Copy link
Copy Markdown
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityIt would be good to show the output right at the print statement, so it is clear that it comes from print(arr). It matters more when there's multiple prints, but I think it helps here too.
Suggested change
Sorry, something went wrong.
All reactions
|
||||||||||||
| ``` | ||||||||||||
| 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] | ||||||||||||
| 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 | ||||||||||||
|
Comment thread
Copy link
Copy Markdown
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityWhat's the purpose of this section? Reading a description of syntax isn't a good way to learn, in my experience. It would be better to just show the i.lower() example, without trying to describe the syntax with words too much.
Sorry, something went wrong.
All reactions
|
||||||||||||
|
|
||||||||||||
| 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 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**] | ||||||||||||
|
Comment thread
Comment on lines
+74
to
+77
Copy link
Copy Markdown
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityAdd . to the end of each. Also consider removing the last item, if you remove the ## Syntax section.
Sorry, something went wrong.
All reactions
|
||||||||||||
| Back | FazBrowse Home | New Git URL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityIt would be good to have this immediately after loops. (You have to change the numbering.)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.