-
Notifications
You must be signed in to change notification settings - Fork 611
New issue
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
Added a List Comprehension topic #34
base: master
Are you sure you want to change the base?
Conversation
Adding additional .md file for list comprehension
Added information to list-comprehension.md
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.
Thanks! A few comments below.
@@ -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) |
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.
It would be good to have this immediately after loops. (You have to change the numbering.)
# List Comprehension | ||
|
||
|
||
## Definition |
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.
This doesn't have to be under a second title.
## Definition |
|
||
|
||
## 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. |
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.
- "Shorter than normal" is a bit ambiguous.
- When performance matters, performance problems don't usually come from small things like this, in my experience. Not really worth mentioning.
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. | |
List comprehensions creates a new list from an existing list using shorter syntax than loops. |
arr = [] | ||
|
||
for i in animals: |
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.
Consider improving the variable names, e.g. arr --> filtered_animals
, i --> animal
, also in the list comprehension below.
print(arr) | ||
``` | ||
```python | ||
['dog', 'cat', 'ape'] |
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.
It 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.
print(arr) | |
``` | |
```python | |
['dog', 'cat', 'ape'] | |
print(arr) # Prints: ['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 |
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.
What'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.
- 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**] |
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.
Add .
to the end of each. Also consider removing the last item, if you remove the ## Syntax
section.
list-comprehension.md
Taking a class on github and I had to contribute to a repository. So I thought contributing to a python-tutorial would be interesting. I can add or change more things if needed.