TransWikia.com

How to iterate a list from the beginning when it reaches the maximum index

Stack Overflow Asked on January 8, 2021

Hi i am new in python and i have a problem. I know there is a way to this this with % but i am trying to do it without the modulo if there is a way to do so. I have a list of numbers and i need to every Nth(i have every second number to keep it simple) number to be printed out, but when is reaches the maximux index it will give me an error "list is out of index". I have tried if original_position >= len(s): for j in s[0:] but it doesnt seems to work. This is what i have come up with so far:

def napln(n):
s = []
for i in range(n):
    s.append(i)
    print(s)
return s

def rozpocitavadlo(s):
rozpocitavadlo = []
for i in s:
    original_position = s[i]
    changed_position = s[i+2]
    original_position = changed_position
    if original_position >= len(s):
        for j in s[0:]:
            original_position = s[j]
            changed_position = s[j+2]
            original_position = changed_position
    rozpocitavadlo.append(s.pop(original_position))

s = napln(6)

3 Answers

If your intent is just to print out one number every two, you can simply run something like:

def rozpocitavadlo(s):
    to_print = s[::2]
    print(to_print)

Adding a 1 in [1::2] your iterating process will start from the second element skipping the first one, as follow:

def rozpocitavadlo(s):
    to_print = s[1::2]
    print(to_print)

If you also want to start over and add the skipped element to the final list you can add first the even number and then the odd ones:

def rozpocitavadlo(s):
    to_print = s[1::2]
    to_print.extend(s[0::2])
    print(to_print)

Your result is going to be: [1, 3, 5, 0, 2, 4]

You can deepen here the slice notation that allow you to iterate over a list of a custom number of elements

Obviously as said by @vishal-gahlot, you can parameterize the number of elements to skip by adding a Nth parameter to the function

def rozpocitavadlo(s, Nth: int):
    to_print = s[::Nth]
    print(to_print)

FINAL: for a more flexible implementation with Nth as a parameter you can iterate over and over the original list until every element is added to the new one:

def rozpocitavadlo(s, nth: int):
    to_print = []
    for i in reversed(range(nth)):
        to_print.extend(s[i::nth])
    print(to_print)

UPDATE AFTER COMMENTS: with this you will get the even number, remove them and than reiterate on the remain element until the last remaining:

def rozpocitavadlo(s):
    to_print = []
    while len(s) > 0:
        if len(s) == 1:
            to_print.extend(s[:])
            break
        to_print.extend(s[1::2])
        del s[1::2]
    print(to_print)

Your output will be: [1, 3, 5, 2, 4, 0]

Correct answer by lorenzozane on January 8, 2021

def rozpocitavadlo(s,Nth): return s[::Nth]

You can use this method to print every nth number of the list

Answered by Vishal Gahlot on January 8, 2021

Your question i have a list of numbers and i need to every Nth(i have every second number to keep it simple) number to be printed out can be solved with built-in Python functions! No need for extra code.

for i in range(0, len(s), 2):
    print(s[i])

The parameters for range are the starting index (0), how many items to loop over (len(s)) and how big the steps should be. This way, Python will print the 0th element and, from there, every second element.

EDIT: You could try something like this:

s = [0, 1, 2, 3, 4, 5]
out = []

for offset in range(len(s) // (len(s) // 2)):
    for i in range(offset, len(s), 2):
        out.append(s[i])
        offset = offset + 1

print(out)

With this, you are looping through the list multiple times with different starting offsets, so you first get all even numbers, and then all odd numbers. The calculation in the offset loop is there to figure out how many times we need to loop through the list.

Answered by klauslippo on January 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP