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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:55:23+00:00 2026-05-31T13:55:23+00:00

I was wondering whether there is a more pythonic (and efficient) way of doing

  • 0

I was wondering whether there is a more pythonic (and efficient) way of doing the following:

MAX_SIZE = 100
nbr_elements = 10000
y = np.random.randint(1, MAX_SIZE, nbr_elements)


REPLACE_EVERY_Nth = 100
REPLACE_WITH = 120
c = 0

for index, item in enumerate(y):
    c += 1
    if (c % REPLACE_EVERY_Nth == 0):
        y[index] = REPLACE_WITH

So basically I generate a bunch of numbers from 1 to MAX_SIZE-1, and then I want to replace every REPLACE_EVERY_Nth element with REPLACE_WITH. This works fine but I guess it could be somehow done without using enumerate?

I was thinking something like this (which I know is wrong, because I replace the original y with the indices of y):

y = map(lambda x: REPLACE_WITH if not x%REPLACE_EVERY_Nth else x, range(len(y)))

is there a way to do modulo on the indices but replace the values?

  • 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-31T13:55:25+00:00Added an answer on May 31, 2026 at 1:55 pm

    Use a slicing with REPLACE_EVERY_Nth as step value:

    y[::REPLACE_EVERY_Nth] = REPLACE_WITH
    

    This is slightly different from your code, since it will start with the very first item (i.e. index 0). To get exactly what your code does, use

    y[REPLACE_EVERY_Nth - 1::REPLACE_EVERY_Nth] = REPLACE_WITH
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've found this code, but I was wondering whether there's a more streamlined way
I was just wondering whether there is some way to do this: I have
I'm wondering whether there is a way to poll a socket in c# when
I'm wondering whether there is a way to find out whether a class is
I've been wondering whether there is a good "git export" solution that creates a
I was wondering whether there is a simple PHP library to test the loading
A recent problem* left me wondering whether there is a text editor out there
I'm fairly new to the STL, so I was wondering whether there are any
I liked the discussion at Differences in Generics , and was wondering whether there
I was wondering whether there is any associated performance problems with using only one

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.