How to loop over a list and roll over at the end?

Question:

I have a list and I must move elements from it to another list, for example, I have this:

lst_one = [1, 2, 3, 4]
iterable = 6
lst_two = []

The result needed is this:

lst_one = []
lst_two = [2, 1, 4, 3]

In this case, the iterable is the steps I move through the list to find the next available number. When I find it, I have to add it to the new list and remove it from the old one. So, if 1 is the first number, I have to run though all the list and if the list is too short, continue counting again from the beginning over and over again, until the number is found and this should continue until there are no elements left in lst_one.
No functions or classes allowed!

I literally cannot start, because the "loop" stops at the end of the list and doesn’t continue again from the beginning

lst = input()
num = int(input())
lst = lst.split(" ")
lst = [eval(i) for i in soldiers_list]
new_list = []

for i in range(num - 1, len(lst), num):
    new_list.append(lst[i])
    del lst[i]
Asked By: Nikoi Nikoi

||

Answers:

Is this what you need?

lst_one = [1, 2, 3, 4]
iterable = 6
lst_two = []
i = 0

while len(lst_one) != 0:
    i = (i+iterable-1) % len(lst_one)
    lst_two.append(lst_one[i])
    del lst_one[i]
Answered By: Jasper Rou

This was an interesting question, OP!

Let’s see the process in detail:

  1. First we keep in mind that we need to deal with all the elements present in lst_one.

    We can do that by storing the length of lst_one before doing anything else, and setting up a for loop with it:

    lst_one = [1, 2, 3, 4]
    iterable = 6
    
    
    lst_one_len = len(lst_one)
    
    for counter in range(lst_one_len):
    
  2. Let’s run through getting the output fpr two elements:

    • First element, iteration 1: 1, index 0
    • First element, iteration 2: 2, index 1
    • First element, iteration 3: 3, index 2
    • First element, iteration 4: 4, index 3
    • First element, iteration 5: 1, index 0 (roll over)
    • First element, iteration 6: 2, index 1

    So, we’re left with 2. Now, upon deleting 2 from the list, we’re left with [1, 3, 4], and we were at index 1, so we continue from there.

    • Second element, iteration 1: 3, index 1
    • Second element, iteration 2: 4, index 2
    • Second element, iteration 3: 1, index 0 (roll over)
    • Second element, iteration 4: 3, index 1
    • Second element, iteration 5: 4, index 2
    • Second element, iteration 6: 1, index 0 (roll over)

    So, we’re left with 1. Now, upon deleting 1 from the list, we’re left with [3, 4], and we were at index 0, so we continue from there.

    And so on…

    So, we need to keep track of the index we’re at.

    We can do that by setting up a variable previous_index and setting it to 0 before the for loop:

    lst_one = [1, 2, 3, 4]
    iterable = 6
    
    
    lst_one_len = len(lst_one)
    previous_index = 0
    
    for counter in range(lst_one_len):
    
  3. Now, imagine that the index in the previous step (just index, not previous_index), kept growing on beyond 4. This would obviously produce an IndexError, so we need to make sure that it doesn’t.

    But this also means that we can grow the index beyond 4 and then "collapse" it to the length of our list later.

    So, we’ll define it as an element index that goes previous_index elements beyond iterable, but we’all also have to subtract 1 to account for the fact that your question assumes indexing starts at 1, not 0:

    lst_one = [1, 2, 3, 4]
    iterable = 6
    
    lst_two = []
    lst_one_len = len(lst_one)
    
    previous_index = 0
    
    for counter in range(lst_one_len):
        index = iterable + previous_index - 1
    
  4. Now, we need to make sure that index doesn’t go beyond the length of the list, and that’s a simple matter of using the modulo operator %:

    lst_one = [1, 2, 3, 4]
    iterable = 6
    
    lst_two = []
    lst_one_len = len(lst_one)
    
    previous_index = 0
    
    for counter in range(lst_one_len):
        index = iterable + previous_index - 1
    
        collapsed_index = index % len(lst_one)
    
  5. Now, we need to add the element at collapsed_index to lst_two and remove it from lst_one:

    lst_one = [1, 2, 3, 4]
    iterable = 6
    
    lst_two = []
    lst_one_len = len(lst_one)
    
    previous_index = 0
    
    for counter in range(lst_one_len):
        index = iterable + previous_index - 1
    
        collapsed_index = index % len(lst_one)
        lst_two.append(lst_one[collapsed_index])
        del lst_one[collapsed_index]
    
  6. And finally, we need to set previous_index to collapsed_index so that we can continue from there. Also print the lists to see the result:

    lst_one = [1, 2, 3, 4]
    iterable = 6
    
    lst_two = []
    lst_one_len = len(lst_one)
    
    previous_index = 0
    
    for counter in range(lst_one_len):
        index = iterable + previous_index - 1
    
        collapsed_index = index % len(lst_one)
        lst_two.append(lst_one[collapsed_index])
        del lst_one[collapsed_index]
    
        previous_index = collapsed_index
    
    print(lst_two)
    
Answered By: eccentricOrange
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.