realpython / realpython/python-guide

Code Style - not really good list manipulation example - needs explanation

Open
#1,086 2 comments 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.