##### Remove specific elements from list with a pattern
 Remove specific elements from list with a pattern Xalagy Unladen Swallow Posts: 2 Threads: 1 Joined: Oct 2020 Reputation: Oct-10-2020, 10:24 PM #Hi everyone, here is my problem: # My Printer-Software got an error while selecting the double printing feature # My initial goal was to print 4 pages on each side of the printing paper #(4 in the front and 4 in the back) #I need a list that follows this Pattern: #[1, 2, 3, 4, 9, 10, 11, 12, 17, 18, 19, 20, 25, 26, 27, 28] (for the front pages) #[5, 6, 7, 8, 13, 14, 15, 16, 21, 22, 23, 24, 29, 30, 31, 32] (for the back pages) # I need to expand this list to 8000 but with a decent code... input = 8000 output = list(range(input + 1)) del output[0] print(output) #Here I am stuck because this list follows this order [1, 2, 3, 4,...]. How can I change it to the desired list structure like above? Reply Posts: 818 Threads: 1 Joined: Mar 2018 Reputation: Oct-10-2020, 11:15 PM (This post was last modified: Oct-10-2020, 11:15 PM by scidam.) Try the following: ```>>> N = 9 >>> sum([list(range(i, j)) for i, j in zip([4*k+1 for k in range(N)][1::2], [4*k + 5 for k in range(N)][1::2])], []) [5, 6, 7, 8, 13, 14, 15, 16, 21, 22, 23, 24, 29, 30, 31, 32] >>> sum([list(range(i, j)) for i, j in zip([4*k+1 for k in range(N)][0::2], [4*k + 5 for k in range(N)][0::2])], []) [1, 2, 3, 4, 9, 10, 11, 12, 17, 18, 19, 20, 25, 26, 27, 28, 33, 34, 35, 36]``` Reply bowlofred Da Bishop Posts: 1,184 Threads: 3 Joined: Mar 2020 Reputation: Oct-11-2020, 01:30 AM Maybe not the most standard way, but I would use compress and cycle here to create two iterators for the front and the back.   ```from itertools import cycle, compress n = 32 front = compress(range(1,n+1), cycle([1]*4 + [0]*4)) rear = compress(range(1,n+1), cycle([0]*4 + [1]*4)) print(f"front: {list(front)}") print(f"rear: {list(rear)}")`````````Output:front: [1, 2, 3, 4, 9, 10, 11, 12, 17, 18, 19, 20, 25, 26, 27, 28] rear: [5, 6, 7, 8, 13, 14, 15, 16, 21, 22, 23, 24, 29, 30, 31, 32]`````` Reply Xalagy Unladen Swallow Posts: 2 Threads: 1 Joined: Oct 2020 Reputation: Oct-11-2020, 07:18 AM (Oct-11-2020, 01:30 AM)bowlofred Wrote: Maybe not the most standard way, but I would use compress and cycle here to create two iterators for the front and the back.   ```from itertools import cycle, compress n = 32 front = compress(range(1,n+1), cycle([1]*4 + [0]*4)) rear = compress(range(1,n+1), cycle([0]*4 + [1]*4)) print(f"front: {list(front)}") print(f"rear: {list(rear)}")`````````Output:front: [1, 2, 3, 4, 9, 10, 11, 12, 17, 18, 19, 20, 25, 26, 27, 28] rear: [5, 6, 7, 8, 13, 14, 15, 16, 21, 22, 23, 24, 29, 30, 31, 32]`````` an elegant way. thank you!!! Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Extracting Elements From A Website List knight2000 2 206 Jul-20-2021, 10:38 AM Last Post: knight2000 Make Groups with the List Elements quest 2 212 Jul-11-2021, 09:58 AM Last Post: perfringo I cannot delete and the elements from the list quest 4 605 May-11-2021, 12:01 PM Last Post: perfringo List of lists - merge sublists with common elements medatib531 1 492 May-09-2021, 07:49 AM Last Post: Gribouillis Find Common Elements in 2 list quest 4 600 Apr-14-2021, 03:57 PM Last Post: quest Sorting Elements via parameters pointing to those elements. rpalmer 3 627 Feb-10-2021, 04:53 PM Last Post: rpalmer .remove() from a list - request for explanation InputOutput007 3 550 Jan-28-2021, 04:21 PM Last Post: InputOutput007 Group List Elements according to the Input with the order of binary combination quest_ 19 1,395 Jan-28-2021, 03:36 AM Last Post: bowlofred Adding List Element if Second part of the List Elements are the Same quest_ 3 616 Nov-25-2020, 04:33 PM Last Post: bowlofred Remove double quotes from the list ? PythonDev 22 2,223 Nov-05-2020, 04:53 PM Last Post: snippsat

Forum Jump:

### User Panel Messages

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