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

  • Home
  • SEARCH
  • 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 961087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:17:09+00:00 2026-05-16T01:17:09+00:00

>>> a=range(5) >>> [a[i] for i in range(0,len(a),2)] ## list comprehension for side effects

  • 0
>>> a=range(5)
>>> [a[i] for i in range(0,len(a),2)] ## list comprehension for side effects
[0, 2, 4]
>>> a
[0, 1, 2, 3, 4]
>>> [a[i]=3 for i in range(0,len(a),2)] ## try to do assignment
SyntaxError: invalid syntax
>>> def setitem(listtochange,n,value):  ## function to overcome limitation
    listtochange[n]=value
    return value

>>> [setitem(a,i,'x') for i in range(0,len(a),2)] ## proving the function
['x', 'x', 'x']
>>> a 
['x', 1, 'x', 3, 'x']   # We did assignment anyway
  • 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-16T01:17:10+00:00Added an answer on May 16, 2026 at 1:17 am

    For my timing mentioned (see also the Improving pure Python prime sieve by recurrence formula)
    from time import clock

    def rwh_primes1(n):
        # https://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python/3035188#3035188
        """ Returns  a list of primes < n """
        sieve = [True] * (n//2)
        for i in xrange(3,int(n**0.5)+1,2):
            if sieve[i//2]:
                sieve[i*i//2::i] = [False] * ((n-i*i-1)//(2*i)+1)
        return [2] + [2*i+1 for i in xrange(1,n/2) if sieve[i]]
    
    def rwh_primes_tjv(n):
        # recurrence formula for length by amount1 and amount2 tjv
        """ Returns  a list of primes < n """
        sieve = [True] * (n//2)
        amount1 = n-10
        amount2 = 6
    
        for i in range(3,int(n**0.5)+1,2):
            if sieve[i//2]:
                 ## can you make recurrence formula for whole reciprocal?
                sieve[i*i//2::i] = [False] * (amount1//amount2+1)
            amount1-=4*i+4
            amount2+=4
    
        return [2] + [2*i+1 for i in xrange(1,n//2) if sieve[i]]
    
    def rwh_primes_len(n):
        """ Returns  a list of primes < n """
        sieve = [True] * (n//2)
    
        for i in range(3,int(n**0.5)+1,2):
            if sieve[i//2]:
                sieve[i*i//2::i] = [False] * len(sieve[i*i//2::i])
    
        return [2] + [2*i+1 for i in xrange(1,n//2) if sieve[i]]
    
    def rwh_primes_any(n):
        """ Returns  a list of primes < n """
        halfn=n//2
        sieve = [True] * (halfn)
    
        for i in range(3,int(n**0.5)+1,2):
            if sieve[i//2]:
                any(sieve.__setitem__(item,False) for item in range(i*i//2,halfn,i))
    
        return [2] + [2*i+1 for i in xrange(1,n//2) if sieve[i]]
    
    
    if __name__ == "__main__":
        n = 1000000
    
        print("rwh sieve1")
        t=clock()
        r=rwh_primes1(n)
        print("Length %i,  %s ms" %(len(r),1000*(clock()-t)))
    
        print("rwh sieve with recurrence formula")
        t=clock()
        r=rwh_primes_tjv(n)
        print("Length %i,  %s ms" %(len(r),1000*(clock()-t)))
    
        print("rwh sieve with len function")
        t=clock()
        r=rwh_primes_len(n)
        print("Length %i,  %s ms" %(len(r),1000*(clock()-t)))
    
        print("rwh sieve with any with side effects")
        t=clock()
        r=rwh_primes_any(n)
        print("Length %i,  %s ms" %(len(r),1000*(clock()-t)))
        raw_input('Ready')
    
    """ Output:
    rwh sieve1
    Length 78498,  213.199442946 ms
    rwh sieve with recurrence formula
    Length 78498,  218.34143725 ms
    rwh sieve with len function
    Length 78498,  257.80008353 ms
    rwh sieve with any with side effects
    Length 78498,  829.977273648 ms
    Ready
    """
    

    Length function and all with setitem are not satisfactory alternatives, but the timings are here to demonstrate it.

    rwh sieve with len function
    Length 78498, 257.80008353 ms

    rwh sieve with any with side effects
    Length 78498, 829.977273648 ms

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

Sidebar

Related Questions

fno = input() myList = list(fno) sum = 0 for i in range(len(fno)): if
this is my code : a=[1,0,None,3] def b(my_list): for i in range(len(my_list)): if not
Following program is used to sort a list for i in range(len(q)): for j
def add(a,b): for i in range(len(a)): a[i] = a[i] + b def main(): amounts
import re , strings , os ,sys sentence = abcdefghijkl for i in range(0,len(sentence),3):
I have got a chunk of code like for i in range(0, len(a)) b[i]
I have a string: Range(T4).Value = Rule 13s voilation I want to write 13s
# max_list = [83, 1350, 1, 100] for i in range(len(max_list)): new_value = 1
My code creats only the last instance times len(list) how can i change this
for row in c: for i in range(len(row)): if i not in keep: del

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.