Reputation: 1277
So...is there an easy way to get next and previous values while iterating with a for-loop in Python?
I know this can be easily done if you do something like:
a = [3,4,5,6,3,4,5]
for x in range(len(a)):
next = a[x+1]
But what about:
for x in a:
x.next??
Upvotes: 4
Views: 152
Reputation: 9858
Probably overkill but I sometimes use the following more general generator for this, which yields a sequence of 'windows' of any size on a list or other iterable. (The window size must be less than the length of the iterable.)
def sliding_window(iterable, size):
try: # indexed iterable
for i in range(len(iterable) - size + 1):
yield iterable[i:i+size]
except TypeError: # iterator
window = [next(iterable) for _ in range(size)]
yield window
for item in iterable:
window = window[1:] + [item]
yield window
a = [3,4,5,6,3,4,5]
for current, following in sliding_window(a, 2):
print(current, following)
Upvotes: 1
Reputation:
You could always convert a
into an iterator with iter
and then iterate over that. This will allow you to use next
inside the loop to advance the iterator that you are iterting over:
>>> a = [3,4,5,6,3,4,5]
>>> it = iter(a)
>>> for i in it:
... j = next(it, None)
... print('i={}, j={}'.format(i, j))
...
i=3, j=4
i=5, j=6
i=3, j=4
i=5, j=None
>>>
Also, the None
in there is the default value to return if there is no next item. You can set it to whatever value you want though. Omitting the argument will cause a StopIteration
exception to be raised:
>>> a = [1, 2, 3, 4, 5]
>>> it = iter(a)
>>> for i in it:
... j = next(it)
... print('i={}, j={}'.format(i, j))
...
i=1, j=2
i=3, j=4
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
StopIteration
>>>
Upvotes: 3
Reputation: 19733
this is easy too:
>>> a = [3,4,5,6,3,4,5]
>>> for i in range(1,len(a)):
... print a[i-1],a[i]
...
3 4
4 5
5 6
6 3
3 4
4 5
Upvotes: 1
Reputation: 2691
If you want both the previous
and the next
element in a circular sequence for each iteration:
a = [3,4,5,6,3,4,5]
l = len(a)
for k, v in enumerate(a):
print a[(k-1)%l], v, a[(k+1)%l] #prints previous, current, next elements
Upvotes: 1
Reputation: 76762
Here is a common pattern that I use to iterate over pairs of items in a sequence:
>>> a = range(10)
>>> for i, j in zip(a, a[1:]):
... print i, j
...
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
If you want three items (prev, item, next) you can do this:
>>> for i, j, k in zip(a, a[1:], a[2:]):
... print i, j, k
...
0 1 2
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
7 8 9
i
is the previous element, j
is the current element, k
is the next element.
Of course, this "starts" at 1 and "ends" at 8. What should you receive as prev/next at the ends? Perhaps None
? Probably easiest to just do this:
>>> a = [None] + a + [None]
>>> for i, j, k in zip(a, a[1:], a[2:]):
... print i, j, k
...
None 0 1
0 1 2
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
7 8 9
8 9 None
Upvotes: 4
Reputation: 7743
easiest way I know of is
for x,next in zip (a, a[1:]):
# now you have x and next available
Upvotes: 3