0

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)
Thomas
  • 3
  • 4

3 Answers3

1

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.

klauslippo
  • 13
  • 4
  • Thanks for the help, but this is not quite what i need, in my mind it was something like `[0, 1, 2, 3, 4, 5]` and every second nubmer will be added to a new list -> `[1, 3, 5, 2, 0, 4]` and from the 5 its out of index and i need the loop to start from the beginning of the remaining numbers, hope i expressed myself undestrandably. – Thomas Nov 11 '20 at 08:55
  • 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. – klauslippo Nov 11 '20 at 10:06
0

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]

lorenzozane
  • 1,214
  • 1
  • 5
  • 15
  • oh sorry, i meant i want to make a new list out of every second number something like this `rozpocitavadlo.append(s.pop(original_position))` – Thomas Nov 11 '20 at 08:28
  • @Thomas well to_print is exactly a new list containing only one out of two number. It's not what you expected? With the slice operator [::2] here your are going to save, going from the beginning to the end of the list, one out of two number in the new to_print list – lorenzozane Nov 11 '20 at 08:30
  • @Thomas to clarify what you said under klauslippo answer (I can't comment under his post), what's the order of the second half of your expected result? I can't see a pattern in the [..., 2, 0, 4] order – lorenzozane Nov 11 '20 at 09:05
  • `[0, 1, 2, 3, 4, 5]` 1 is second is the list -> `[1]`, 3 is second in the list after 1 -> `[1, 3]`, 5 is second in the list after 3 -> `[1, 3, 5]`, here it should start looping from the 0 index so the second number can 2 after 5 -> `[1, 3, 5, 2]` and so on. The thing is i cant figure out how loop from the 0 index of the reminang numbers `[0, 2, 4]` my attempt you see above gives me the "list is out if index", – Thomas Nov 11 '20 at 09:19
  • @Thomas understood, just one last thing and I will help you to resolve it. After getting the `2`, since there are no more elements again, you want to start over iterating on `[0, 4]`? And then get the remaining `[0]`? Cause if yes the final result will be `[1, 3, 5, 2, 4, 0]`. Or would you proceed in a different way? – lorenzozane Nov 11 '20 at 09:28
  • thank you very much and i have a question what is the does the `reversed` in the loop and `extetend` in `to_print.extend(s[i::nth])` do? as i said before i am new in python so i dont know there – Thomas Nov 11 '20 at 09:31
  • @Thomas The `reversed` allow you to iterate with in starting from the last element of the `range` up to `0`. The scope of that piece of cose is to get at the first iteration the `2nd, 4th, ...` element, and than `1st, 3rd, ...` Obviously that would be usefull if `nth` is not 2, but, for exaple 3, so the iteration will be: Get `3rd` and `6th` element, then `2nd` and `5th`, and at the end the `1st` and `4th` – lorenzozane Nov 11 '20 at 09:39
  • yes, start looping from the remaning numbers but it should start couting from the 2 so 4 is first and 0 is second as you would normally at least i did that. `[0, 1, 2, 3, 4, 5]` 1 is second is the list -> [`1]` , 3 is second in the list after 1 -> `[1, 3]`, 5 is second in the list after 3 -> `[1, 3, 5]`, 2 is second after 5 ->`[1, 3, 5, 2]`, the remaining `[0,4]` the 4 should be the firt one and 0 the second one because the counting started at nubner 2, hope you understand what i am trying to say here, its quite gard to tell that just show it to you at least for me – Thomas Nov 11 '20 at 09:43
  • @Thomas check out the last update to the answer and see if it's the specific thing you need! – lorenzozane Nov 11 '20 at 09:43
  • the final i what i needed, the update is not quite what i imaged but thank you very for the help – Thomas Nov 11 '20 at 10:07
  • @Thomas ok perfect then! If you could mark the answer as accepted would be great! Have a good day – lorenzozane Nov 11 '20 at 10:15
0

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

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