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 9148211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:12:45+00:00 2026-06-17T11:12:45+00:00

I have a list: mylist = [0, 0, 0, 0, 0] I only want

  • 0

I have a list: mylist = [0, 0, 0, 0, 0]

I only want to replace selected elements, say the first, second, and fourth by a common number, A = 100.

One way to do this:

mylist[:2] = [A]*2
mylist[3] = A
mylist
[100, 100, 0, 100, 0]

I am looking for a one-liner, or an easier method to do this. A more general and flexible answer is preferable.

  • 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-06-17T11:12:46+00:00Added an answer on June 17, 2026 at 11:12 am

    Especially since you’re replacing a sizable chunk of the list, I’d do this immutably:

    mylist = [100 if i in (0, 1, 3) else e for i, e in enumerate(mylist)]
    

    It’s intentional in Python that making a new list is a one-liner, while mutating a list requires an explicit loop. Usually, if you don’t know which one you want, you want the new list. (In some cases it’s slower or more complicated, or you’ve got some other code that has a reference to the same list and needs to see it mutated, or whatever, which is why that’s “usually” rather than “always”.)

    If you want to do this more than once, I’d wrap it up in a function, as Volatility suggests:

    def elements_replaced(lst, new_element, indices):
        return [new_element if i in indices else e for i, e in enumerate(lst)]
    

    I personally would probably make it a generator so it yields an iteration instead of returning a list, even if I’m never going to need that, just because I’m stupid that way. But if you actually do need it:

    myiter = (100 if i in (0, 1, 3) else e for i, e in enumerate(mylist))
    

    Or:

    def elements_replaced(lst, new_element, indices):
        for i, e in enumerate(lst):
            if i in indices:
                yield new_element
            else:
                yield e
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list in R some 10,000 elements long. Say I want to
Say I have: public ViewResult List() { IEnumerable<IModel> myList = repository.GetMyList(); if(1 == myList.Count())
I have 10 items in drop down and want to display only first 5
I have this list: mylist = [20, 30, 25, 20, 30] After getting the
I have a simple list: mylist = [foo, bar, baz] and a QtTableWidget with
If I have some R list mylist , you can append an item obj
I have a list of items sorted alphabetically: mylist = [a,b,c,d,e,f,g,h,i,j] I'm able to
I have a list to which I append Axes3D plots. Like this: self.myList.append(self.axes.plot(xValues, yValues,
I have a list, with sub-lists of items like this. mylist = [ ['ITEM
I have a Arraylist List<?> myList=new ArrayList(); myList = fetchQuery(); //fetches the list of

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.