stackOverflow
stackOverflow

Reputation: 23

How to remove every nth term in a Python list

I need to solve a problem using Python. I can solve the problem without code and if you search the internet the answer is public. I am having trouble in getting my code to work to solve this question. Anyways, here is the question:

Steve wrote the digits 1, 2, 3, 4, and 5 in order repeatedly from left to right, forming a list of 10,000 digits, beginning 123451234512.... He then erased every third digit from his list (that is, the 3rd, 6th, 9th, ... digits from the left), then erased every fourth digit from the resulting list (that is, the 4th, 8th, 12th, ... digits from the left in what remained), and then erased every fifth digit from what remained at that point. What is the sum of the three digits that were then in the positions 2019, 2020, 2021?

I have written out the python code to print out all the numbers into a list. I need to figure out how to remove every nth digit.

list = []
value = 1
for i in range (10000):
    list.append (value)
    value = value + 1
    if value == 6:
        value = 1

That is the code for writing out the first 10,000 digits.

In a previous class I have written out a code to remove every nth term and prints it out. That code is shown below:

n = 3
def RemoveThirdNumber(int_list):


    pos = n - 1
    index = 0
    len_list = (len(int_list))

    while len_list > 0:

        index = (pos + index) % len_list

        print(int_list.pop(index))
        len_list -= 1

nums = [1, 2, 3, 4]
RemoveThirdNumber(nums)


print(list)

I need help in changing that code so it goes through the list once removing every third term and prints out the remaining numbers.

So that means that instead of the output being

3
2
4
1

it will be

[1,2,4]

Thanks for trying to help!

Upvotes: 0

Views: 351

Answers (6)

Paul M.
Paul M.

Reputation: 10799

Here's a solution I came up with. I'm not a fan of having to turn each slice into a tuple, just to consume from the iterator and have chunk potentially equal something falsey. Maybe someone can look it over and let me know if I've made a mistake somewhere? Or just suggest other cute itertools recipes. Apparently, the sum of the three digits is 10:

from itertools import cycle, islice

digits = islice(cycle([1, 2, 3, 4, 5]), 10000)

def skip_nth(iterable, n):
    while chunk := tuple(islice(iterable, n)):
        yield from islice(chunk, n-1)

sum_of_digits = sum(islice(skip_nth(skip_nth(skip_nth(digits, 3), 4), 5), 2019, 2022))
print(sum_of_digits)

Output:

10

EDIT - As per Matthias' suggestion:

def skip_nth(iterable, n):
    yield from (value for index, value in enumerate(iterable) if (index + 1) % n)

Upvotes: 1

Lior Cohen
Lior Cohen

Reputation: 5735

a = [1,2,3,4,5] * (10000//5)
a = [a[i] for i in range(len(a)) if i%3 != 2]
a = [a[i] for i in range(len(a)) if i%4 != 3]
a = [a[i] for i in range(len(a)) if i%5 != 4]

print(a[2019:2022])  # --> [2, 5, 3]
print(sum(a[2019:2022]))  # --> 10

Upvotes: 1

MrLok3rs
MrLok3rs

Reputation: 3

First thing- try to not use Python built in names as "list" to name your variables. It may cause problems.

Try to use filter method.

values_list = []
value = 1
for i in range(10000):
    values_list.append(value)
    value = value + 1
    if value == 6:
        value = 1

filtered = filter(lambda x: (values_list.index(x) + 1) % 3 != 0, values_list)

filtered_to_list = [element for element in filtered]

print(filtered_to_list)

FIlter returns generator, that's why used list comprehension to get its elements. Lambda functions checks if it is third element of list ( + 1 to its index, because indexing starts at 0 and 0 % 0 == 0, so then it'll remove 1st element as well)

I'm also newbie, but I hope that was helpful.

Upvotes: 0

DeeStarks
DeeStarks

Reputation: 329

I hope I understand your question. If I do, instead of writing all of that, why not write fewer codes if you're going to remove every third item. And, avoid reserved words..instead of using 'list' as a variable name, use something like 'lst' or something else

lst = []
val = 1
for i in range(10000):
    lst.append(val)
    val = val + 1
    if val == 6:
        val = 1

def remove_third_item(your_list):
  for i in your_list:
      if i%3==0:
          your_list.remove(i)
  return your_list

print(remove_third_item(lst))

Upvotes: 0

Leemosh
Leemosh

Reputation: 905

Tested with 100 only.

bigList = []
value = 1
for i in range (100):
    biglist.append(value)
    value += 1
    if value == 6:
        value = 1
# if you print here you get [1,2,3,4,5,1,2,3...]
x = 3 
del biglist[x-1::x] #where x is the number of "steps" - in your case 3 
# if you print here - biglist ==> 1,2,4,5,2,3 etc.. 

Upvotes: 0

Andreas B
Andreas B

Reputation: 42

An inplementation could be this right here.

def RemoveThirdNumber(int_list,every):
    rList=[]
    i=0
    for element in int_list:
        i+=1
        if i%every!=0:
            rList.append(element)
    return rList
    
nums = range(1,101)

print(RemoveThirdNumber(nums,3))

Upvotes: 0

Related Questions