GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Code Style - not really good list manipulation example - needs explanation · Issue #1086 · realpython/python-guide

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

New issue
Copy link
New issue
Copy link
Open
Open
Code Style - not really good list manipulation example - needs explanation#1086
Copy link

Description

@vbrozik
vbrozik
opened on Jan 6, 2021
Issue body actions

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.

Activity

  1. dbader commented on Jan 6, 2021

    @dbader
    dbader
    on Jan 6, 2021
    Member
    More actions

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

  2. vbrozik commented on Feb 12, 2021

    @vbrozik
    vbrozik
    on Feb 12, 2021
    Author
    More actions

    Excuse my delayed reply. I am still learning Python a lot and in many cases I do not know what is the best way to perform certain task in the language. Also I am not sure if I know English good enough to contribute to an English book. So certainly I need a discussion first :)

    Regarding the example above I would explain:

    For large lists it could be inappropriate to create a new list to replace the old one because of memory requirements. In such cases the good solution is to modify the list in-place:

    for i, _ in enumerate(a):
        a[i] += 3

    PS: Originally I thought that the code below would be the right solution but as the original "good" solution it also creates a new list and only then it assigns its elements to the original list.

    a[:] = (i + 3 for i in a)

    See:

    • https://stackoverflow.com/questions/53286531/assign-iterators-to-a-python-slice
    • https://stackoverflow.com/questions/11877212/python-slice-assignment-of-generator-to-list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.