Home > database >  How to iterate completely through a list after starting in the middle?
How to iterate completely through a list after starting in the middle?

Time:03-22

New to python. How do I iterate completely through a list after starting in the middle (or at any point)? for example, if I have:

[a,b,c,d,e,f,g]

I want to start at d, then go e, f, g, a, b, c. But I am not sure how to do this. I want something to be capable of also going b, c, d, e, f, g, a, or any order that iterates completely through from any starting point.

I found this thread discussing something similar, and a comment solved that problem by:

>>> a = [1, 2, 3, 4, 5, 6, 7]
>>> [a[(len(a)   (~i, i)[i%2]) // 2] for i in range(len(a))]
[4, 5, 3, 6, 2, 7, 1]

But, honestly, I don't know how to interpret how this code accomplishes this task. I do think that whatever I am looking for would be similar to this.

CodePudding user response:

You might try this approach:

>>> a = [1, 2, 3, 4, 5, 6, 7]
>>> start = 3
>>> [a[(start   j) % len(a)] for j in range(len(a))]
[4, 5, 6, 7, 1, 2, 3]

Another approach (this is unnecessary complex under the hood):

>>> a[start:]   a[:start]
[4, 5, 6, 7, 1, 2, 3]

CodePudding user response:

In this solution i use range(len(items)) to generate a iterable object with values from 0 to the length of the list minus 1

That means that in this example in the for loop the value of the value of i will be 0,1,2,3... so i startIndex will be 3, 4,5,...

This is almost what we want but we need to make sure we don't miss the elements at the beginning of the list. The % operator gets the remainder when (i startIndex) is divided by the length of the list. This ensures the rest of the items are printed once i becomes greater than or equal to the length of the list as the remainder will be 0, 1, 2 ...

items = ["a", "b", "c", "d", "e", "f", "g"]
startIndex = 3

for i in range(len(items)):
    print(items[(i   startIndex) % len(items)])

CodePudding user response:

As has been pointed out, you can just add together two slices:

>>> a[3:]   a[:3]
[4, 5, 6, 7, 1, 2, 3]

but the operator creates a new list (even if you're only going to iterate through it once), which may not be desirable if the list is extremely long.

If you want the simplicity of the slice syntax and you want to be able to do a single iteration over the list elements without copying everything into a new list, there's itertools.chain, which lets you chain multiple iterables together without concatenating them into an in-memory list:

>>> import itertools
>>> for n in itertools.chain(a[3:], a[:3]):
...     print(n)
...
4
5
6
7
1
2
3
  • Related