##### Modifying / extracting multiple list items simultaneously using variable from range
 Modifying / extracting multiple list items simultaneously using variable from range ehammarlund Unladen Swallow Posts: 3 Threads: 1 Joined: Dec 2017 Reputation: 0 Dec-06-2017, 06:02 PM (This post was last modified: Dec-06-2017, 06:04 PM by ehammarlund.) I am a beginner(**) trying to figure out how to put multiple items in/out of lists using variables. To demonstrate, I'm trying to make a prime sieve. [u]My comments/questions bolded below. ```highnum=1000000 #the number you’re checking to, here it's a million, could be anything erik=list(range(1,highnum) #create a list to sieve it def sieve(x): erik[(2x)::x]=False # ```I CAN’T FIGURE OUT HOW TO ASSIGN MULTIPLE LIST VALUES AT THE SAME TIME. CAN IT BE DONE? It doesn’t seem to want to accept a variable in the list definition so perhaps not…? I ALSO TRIED: `erik.insert[(2x)::x]=False`THAT DID NOT WORK EITHER. SIGH. IF I CAN GET IT TO WORK, THEN IN THEORY I CAN DO THIS: ```for y in range (1,((highnum/2)+1)): sieve(y) sieveoutput=(Erik[] where erik!= False)``` #WILL THIS WORK/WHY WON’T THIS WORK? HELP! OR DO I NEED TO DO IT LIKE THIS? ```For z in range(1,highnum): iferik(z) != False: sieveoutput.append erik[z]```Sorry about the very low-level questions;I literally started to learn Python three days ago. Thanks in advance for any pointers. Reply hshivaraj Lumberjack Posts: 100 Threads: 13 Joined: Nov 2017 Reputation: 4 Dec-06-2017, 06:34 PM ```def sieve(x): erik[(2x)::x]=False```Im not sure what your trying to do here. In fact what your trying to do there is syntactically wrong! Quote:I CAN’T FIGURE OUT HOW TO ASSIGN MULTIPLE LIST VALUES AT THE SAME TIME. CAN IT BE DONE? It doesn’t seem to want to accept a variable in the list definition so perhaps not…? Did you mean something like this? ```>>> a = list(range(100)) >>> >>> a = [False] * 100 >>> >>> a [False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False] ``` Reply ehammarlund Unladen Swallow Posts: 3 Threads: 1 Joined: Dec 2017 Reputation: 0 Dec-06-2017, 06:46 PM (This post was last modified: Dec-06-2017, 06:46 PM by ehammarlund.) Thanks! I am trying to begin at index value= 2x (whatever x is as per the range loop) and replace the number in the list with "False" throughout the whole list, every X steps. As an example for x=3, the list 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 would become(new changes underlined) 1,2,3,4,5,False,7,8,False,10,11,False,13,14,False,16,17,False,19,20 and if I then ran that same function for x=5 it would become (new changes underlined): 1,2,3,4,5,False,7,8,False,False,11,False,13,14,False,16,17,False,19,False (yes, I know that my actual code includes x=2 and x=4, but I left them out because they make it hard to illustrate what I'm doing.) Eventually the list would only contain "False" and primes. I can then either output a list of primes if I want them or can leave the list as-is for a very quick way to check the primacy of a number. If I succeed, I'll choose which output to use later on. I don't know yet whether there's a material speed difference in a) testing "is list[index]==False" versus b) testing "is [item] contained in list" Reply nilamo Last Thursdayist Posts: 3,290 Threads: 94 Joined: Sep 2016 Reputation: 138 Dec-06-2017, 08:06 PM Quote: As an example for x=3, the list 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 would become(new changes underlined) 1,2,3,4,5,False,7,8,False,10,11,False,13,14,False,16,17,False,19,20 You can assign to a list slice, but only using another list of the same length of the slice you're replacing.  So for this, it'd look something like:```>>> items = list(range(21)) >>> items [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] >>> replace = 3 >>> items[::replace] [0, 3, 6, 9, 12, 15, 18] >>> slice = items[::replace] >>> items[::replace] = [False for _ in range(len(slice))] >>> items [False, 1, 2, False, 4, 5, False, 7, 8, False, 10, 11, False, 13, 14, False, 16, 17, False, 19, 20]``` Reply ehammarlund Unladen Swallow Posts: 3 Threads: 1 Joined: Dec 2017 Reputation: 0 Dec-06-2017, 08:15 PM aaaaaah. That explains so much--thanks a ton!! Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Matching variable to a list index Gilush 17 353 Nov-30-2020, 01:06 AM Last Post: Larz60+ Count number of occurrences of list items in list of tuples t4keheart 1 118 Nov-03-2020, 05:37 AM Last Post: deanhystad concatenating 2 items at a time in a python list K11 3 174 Oct-21-2020, 09:34 AM Last Post: buran List index out of range error while accessing 2 lists in python K11 2 268 Sep-29-2020, 05:24 AM Last Post: K11 list vs [] with range quazirfan 5 329 Sep-19-2020, 12:45 PM Last Post: deanhystad Select the other of 2 items in a list Clunk_Head 7 464 Sep-01-2020, 05:27 PM Last Post: Clunk_Head Removing items from list if containing a substring pythonnewbie138 2 370 Aug-27-2020, 10:20 PM Last Post: pythonnewbie138 List index out of range error when attempting to make a basic shift code djwilson0495 4 460 Aug-16-2020, 08:56 PM Last Post: deanhystad Print the number of items in a list on ubuntu terminal buttercup 2 438 Jul-24-2020, 01:46 PM Last Post: ndc85430 How to put the items of one list in new generated lists Bobbear 1 322 Jun-12-2020, 06:08 AM Last Post: buran

Forum Jump:

### User Panel Messages

##### Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020