FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Added a List Comprehension topic by myckellr · Pull Request #34 · Akuli/python-tutorial · GitHub

Added a List Comprehension topic - #34

Open
myckellr wants to merge 6 commits into
Akuli:masterfrom
myckellr:master
Open

Added a List Comprehension topic#34
myckellr wants to merge 6 commits into
Akuli:masterfrom
myckellr:master

Conversation

myckellr commented Dec 3, 2021

Copy link
Copy Markdown

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.

Akuli left a comment

Copy link
Copy Markdown
Owner

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 Quality

Thanks! A few comments below.

Comment thread basics/README.md
19. [Exceptions](exceptions.md)
20. [Classes](classes.md)
21. [Docstrings](docstrings.md)
22. [List Comprehension](list-comprehension.md)

Copy link
Copy Markdown
Owner

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 Quality

It would be good to have this immediately after loops. (You have to change the numbering.)

# List Comprehension


## Definition

Copy link
Copy Markdown
Owner

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 Quality

This doesn't have to be under a second title.

Suggested change
## 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.

Copy link
Copy Markdown
Owner

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 Quality
  • "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.
Suggested change
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.

Comment on lines +11 to +13
arr = []

for i in animals:

Copy link
Copy Markdown
Owner

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 Quality

Consider improving the variable names, e.g. arr --> filtered_animals, i --> animal, also in the list comprehension below.

Comment on lines +17 to +20
print(arr)
```
```python
['dog', 'cat', 'ape']

Copy link
Copy Markdown
Owner

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 Quality

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.

Suggested change
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

Akuli Dec 3, 2021
edited
Loading

Copy link
Copy Markdown
Owner

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 Quality

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.

Comment on lines +74 to +77
- 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**]

Copy link
Copy Markdown
Owner

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 Quality

Add . to the end of each. Also consider removing the last item, if you remove the ## Syntax section.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants


Back | FazBrowse Home | New Git URL