Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7590515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:25:14+00:00 2026-05-30T20:25:14+00:00

Given the following example code: def myfunc(item): if item == 2: item = 1

  • 0

Given the following example code:

def myfunc(item):
  if item == 2:
    item = 1

mylist = [1,2,3]
for i in mylist:
  myfunc(i)
print(mylist) # output is [1, 2, 3]
# desired output is [1, 1, 3]

I would like to have a function that is called for some or all elements of a list. This function should be able to alter these elements.

What would be the the cleanest solution for this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:25:15+00:00Added an answer on May 30, 2026 at 8:25 pm

    As you’ve written myfunc, working on individual items, it cannot modify the list in place. This is a good thing.

    myfunc receives the name item bound to some value. You can rebind the name item to a new value, but that of course doesn’t affect any other bindings to the same value. All a list is is an ordered sequence of bindings to values, so to change what it contains you have to rebind some of the indexes in the list to new values (or change the values themselves, but numbers can’t be changed; there’s no way to change the number 1 into the number 2, you can only rebind something that referred to the number 1 to refer to the number 2 instead). With no reference to the list, there’s no way for myfunc to change the bindings in the list.

    But myfunc shouldn’t have to care about the list. It works on items, and it doesn’t care whether you got those items from a list, or a dictionary, or read them from a file, or whatever. This is what I said is a good thing about myfunc not being able to modify your list; otherwise you’d have to rewrite myfunc for every different context your items might appear in.

    Instead, just have myfunc return the new value:

    def myfunc(item):
      if item == 2:
        item = 1
      return item
    

    Now the code that calls myfunc can do what you want. This code is aware that the items are being fetched from a list, and that you’re trying to transform the list in place by replacing each item with whatever myfunc does to it. So this is the place you should implement that intention; trying to push it down into myfunc is not the way to keep complexity away from myfunc.

    mylist = [1,2,3]
    for idx, item in enumerate(mylist):
        mylist[idx] = myfunc(item)
    

    If you find you’re repeating this pattern all the time for lots of different functions, you can abstract that out into another function:

    def inplace_map(items, func):
        for i, item in enumerate(items):
            items[i] = func(item)
    

    Now you’ve defined the transformation logic once, and each different item transformation once (and you can re-use the item transformation functions in other contexts than list transformations), and for any given place where you want to transform the items in a list in-place through a function all you have to do is call inplace_map, which very clearly states what you’re doing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following example, why do I have to explicitly use the statement b->A::DoSomething()
Given the following code, How would you refactor this so that the method search_word
Given the following code: def slope(x1, y1, x2, y2): >>> slope(5, 3, 4, 2)
I'm following the example given in http://www.blog.pythonlibrary.org/2010/05/22/wxpython-and-threads/ I have a thread which is checking
I have the following code: def produceCombinations(text, maximumWindowWidth) combinations = [] for windowWidth in
Given the following example (using JUnit with Hamcrest matchers): Map<String, Class<? extends Serializable>> expected
Given the following example data: Users +--------------------------------------------------+ | ID | First Name | Last
Given the following simple example: List<string> list = new List<string>() { One, Two, Three,
Given the following HTML example... <div id='div1'>div one</div> <div id='div2'>div two</div> ...I found that
Given a sorted vector with a number of values, as in the following example:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.