realpython / realpython/python-guide
Code Style - not really good list manipulation example - needs explanation
Nobody has claimed this yet.
- Dominant language
- Batchfile
- Stars
- 29.8k
- Forks
- 5.9k
- PR merge metrics
- No merged PRs in 30d
Description
In the Code Style chapter examples - Short Ways to Manipulate Lists there is an example "Add three to all list members."
"bad"
for i in range(len(a)):
a[i] += 3
"good"
a = [i + 3 for i in a]
Unfortunately the "bad" and "good" examples do a different things:
- "bad" modifies the original list in place
- "good" creates a new modified list and does not change the original object - not exactly "Add three to all list members."
Although the "good" code looks cleaner the result could be unwanted for example in case of huge lists or required side-effect of modifying the original list. The code at least needs an explanation.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Open the Code Style chapter at writing/style.html and read the “Short Ways to Manipulate Lists” section. Compare the two list examples and update the surrounding explanation so their different in-place and new-list behavior is clear; done means readers understand when each example changes the original list.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100