The Wayback Machine - https://web.archive.org/web/20210119193554/https://github.com/realpython/python-guide/issues/1086
Skip to content
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

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

Open
vbrozik opened this issue Jan 6, 2021 · 1 comment
Open

Comments

@vbrozik
Copy link

@vbrozik vbrozik commented Jan 6, 2021

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.

@dbader
Copy link
Member

@dbader dbader commented Jan 6, 2021

Thanks for flagging this -- want to send in a pull-request to update that section?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.