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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:20:25+00:00 2026-05-25T02:20:25+00:00

If I have a list: to_modify = [5,4,3,2,1,0] And then declare two other lists:

  • 0

If I have a list:

to_modify = [5,4,3,2,1,0]

And then declare two other lists:

indexes = [0,1,3,5]
replacements = [0,0,0,0]

How can I take to_modify‘s elements as index to indexes, then set corresponding elements in to_modify to replacements, i.e. after running, indexes should be [0,0,3,0,1,0].

Apparently, I can do this through a for loop:

for ind in to_modify:
    indexes[to_modify[ind]] = replacements[ind]

But is there other way to do this?
Could I use operator.itemgetter somehow?

  • 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-25T02:20:26+00:00Added an answer on May 25, 2026 at 2:20 am

    The biggest problem with your code is that it’s unreadable. Python code rule number one, if it’s not readable, no one’s gonna look at it for long enough to get any useful information out of it. Always use descriptive variable names. Almost didn’t catch the bug in your code, let’s see it again with good names, slow-motion replay style:

    to_modify = [5,4,3,2,1,0]
    indexes = [0,1,3,5]
    replacements = [0,0,0,0]
    
    for index in indexes:
        to_modify[indexes[index]] = replacements[index]
        # to_modify[indexes[index]]
        # indexes[index]
        # Yo dawg, I heard you liked indexes, so I put an index inside your indexes
        # so you can go out of bounds while you go out of bounds.
    

    As is obvious when you use descriptive variable names, you’re indexing the list of indexes with values from itself, which doesn’t make sense in this case.

    Also when iterating through 2 lists in parallel I like to use the zip function (or izip if you’re worried about memory consumption, but I’m not one of those iteration purists). So try this instead.

    for (index, replacement) in zip(indexes, replacements):
        to_modify[index] = replacement
    

    If your problem is only working with lists of numbers then I’d say that @steabert has the answer you were looking for with that numpy stuff. However you can’t use sequences or other variable-sized data types as elements of numpy arrays, so if your variable to_modify has anything like that in it, you’re probably best off doing it with a for loop.

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

Sidebar

Related Questions

I have a list of checkpoints and then a run a function. I originally
I have a list in witch I have only money amounts (numbers), then I
I have List with n items. I wish transform my list to new list,
I have List I want to sort Desc by Priority, which is int and
So I have binded a List to a ListView where the List has items
I have a list box control that contains enough items to list them with
I want to have my list (nav) align to the center of an image
I have a list of changes to a list - Adds and Deletes. The
I have list of catalog paths and need to filter out some of them.
i have list of rows that user select and i want to delete them,

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.