Accessing next/prev element while for looping

J

Joseph Garvin

When I first came to Python I did a lot of C style loops like this:

for i in range(len(myarray)):
print myarray

Obviously the more pythonic way is:

for i in my array:
print i

The python way is much more succinct. But a lot of times I'll be looping
through something, and if a certain condition is met, need to access the
previous or the next element in the array before continuing iterating. I
don't see any elegant way to do this other than to switch back to the C
style loop and refer to myarray[i-1] and myarray[i+1], which seems
incredibly silly given that python lists under the hood are linked
lists, presumably having previous/next pointers although I haven't
looked at the interpeter source.

I could also enumerate:

for i, j in enumerate(myarray):
print myarray, j # Prints each element twice

And this way I can keep referring to j instead of myarray, but I'm
still forced to use myarray[i-1] and myarray[i+1] to refer to the
previous and next elements. Being able to do j.prev, j.next seems more
intuitive.

Is there some other builtin somewhere that provides better functionality
that I'm missing?
 
N

Nick Craig-Wood

Joseph Garvin said:
When I first came to Python I did a lot of C style loops like this:

for i in range(len(myarray)):
print myarray

Obviously the more pythonic way is:

for i in my array:
print i

The python way is much more succinct. But a lot of times I'll be looping
through something, and if a certain condition is met, need to access the
previous or the next element in the array before continuing iterating. I
don't see any elegant way to do this other than to switch back to the C
style loop and refer to myarray[i-1] and myarray[i+1], which seems
incredibly silly given that python lists under the hood are linked
lists, presumably having previous/next pointers although I haven't
looked at the interpeter source.


I think you'll find python lists are actually arrays not linked
lists...
I could also enumerate:

for i, j in enumerate(myarray):
print myarray, j # Prints each element twice

And this way I can keep referring to j instead of myarray, but I'm
still forced to use myarray[i-1] and myarray[i+1] to refer to the
previous and next elements. Being able to do j.prev, j.next seems more
intuitive.


Boundary conditions are a perenial problem in this sort of thing, what
to do at the start / end of the list...
Is there some other builtin somewhere that provides better functionality
that I'm missing?

I suppose you could use itertools...
....
0 1 2
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
7 8 9
 
S

Steven D'Aprano

When I first came to Python I did a lot of C style loops like this:

for i in range(len(myarray)):
print myarray

Obviously the more pythonic way is:

for i in my array:
print i

The python way is much more succinct. But a lot of times I'll be looping
through something, and if a certain condition is met, need to access the
previous or the next element in the array before continuing iterating.


If you need to do that, then either you need to rethink your algorithm so
that you only need to access one element at a time, or you can to change
your loop so that you aren't accessing one element at a time.

For example, you might grab elements three at a time, and if the middle
one meets a certain condition process them, and if not, do nothing.

There is no shortage of ways to handle your situation. However, I can't
help but feel that you probably need to rethink what you are trying to do.
I can't speak for others, but I've never come upon a situation where I
needed to access the element before and the element after the current one.

[thinks...] Wait, no, there was once, when I was writing a parser that
iterated over lines. I needed line continuations, so if the line ended
with a backslash, I needed to access the next line (and potentially the
line after that, and so forth indefinitely). I dealt with that by keeping
a cache of lines seen, adding to the cache if the line ended with a
backslash.
I
don't see any elegant way to do this other than to switch back to the C
style loop and refer to myarray[i-1] and myarray[i+1], which seems
incredibly silly given that python lists under the hood are linked
lists, presumably having previous/next pointers although I haven't
looked at the interpeter source.

Python lists aren't linked lists? They are arrays.

I could also enumerate:

for i, j in enumerate(myarray):
print myarray, j # Prints each element twice


j is a built-in object used to make complex numbers. Or at least it
was, until you rebound it to the current element from myarray. That's bad
practice, but since using complex numbers is rather unusual, one you will
probably get away with.

And this way I can keep referring to j instead of myarray, but I'm
still forced to use myarray[i-1] and myarray[i+1] to refer to the
previous and next elements. Being able to do j.prev, j.next seems more
intuitive.


But that can't work, because j has no knowledge of the list it is part of:

myarray = ["spam", "parrot", "Spanish Inquisition"]
when i = 1, j = "parrot"

Since j is a string, it has no prev or next methods. How could "parrot"
possibly know that "spam" was the previous element?

You can't even have prev and next methods of the list myarray, because it
has no concept of the current element. The current element i is known by
the iterator used by the for loop, not the list it is iterating over.

You could possibly sub-class list, turning it into an iterator-like object
with next and prev methods, but be aware that "next" method has special
meaning to iterators.

You can probably create a generator or iterator that will do what you
want, but I suspect it won't be exactly straightforward.
 
S

Steven D'Aprano

Python lists aren't linked lists? They are arrays.

[slaps head for the stupid typo]
That should have been a full stop, not question mark. Python lists are not
linked lists, period.
 
M

Max Erickson

j> is a built-in object used to make complex numbers. Or at least it
was, until you rebound it to the current element from myarray. That's bad
practice, but since using complex numbers is rather unusual, one you will
probably get away with.

Is it?

Traceback (most recent call last):
File "<pyshell#0>", line 1, in -toplevel-
j
NameError: name 'j' is not defined
I am actually curious, as it doesn't appear to be, but you are usually
'right' when you say such things...

Max
 
S

Steven D'Aprano


[snip example]

Well well, I'll be a monkey's uncle. You learn something new everyday.

Okay, I was wrong. There is no conflict between using j as a name and the
built-in j-as-syntax-for-complex-numbers, except for any potential
conflict in the programmers head.

That makes me feel a lot less guilty about all the times I wrote for j in
range loops :)
 
B

Bas

Just make a custom generator function:
it = iter(seq)
prev = it.next()
cur = it.next()
for next in it:
yield (prev,cur,next)
prev,cur = cur, next

print a,b,c


0 1 2
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
7 8 9

Cheers,
Bas
 
B

Bengt Richter

When I first came to Python I did a lot of C style loops like this:

for i in range(len(myarray)):
print myarray

Obviously the more pythonic way is:

for i in my array:
print i

The python way is much more succinct. But a lot of times I'll be looping
through something, and if a certain condition is met, need to access the
previous or the next element in the array before continuing iterating. I
don't see any elegant way to do this other than to switch back to the C
style loop and refer to myarray[i-1] and myarray[i+1], which seems
incredibly silly given that python lists under the hood are linked
lists, presumably having previous/next pointers although I haven't
looked at the interpeter source.

I could also enumerate:

for i, j in enumerate(myarray):
print myarray, j # Prints each element twice

And this way I can keep referring to j instead of myarray, but I'm
still forced to use myarray[i-1] and myarray[i+1] to refer to the
previous and next elements. Being able to do j.prev, j.next seems more
intuitive.

Is there some other builtin somewhere that provides better functionality
that I'm missing?

I don't know of a builtin, but you could make an iterator that gives you
your sequence as (prev, curr, next) item tuples, where curr is the normal
single item in for curr in my_array: ... e.g.,
... seqiter = iter(seq)
... prev = curr = NULL
... try: next = seqiter.next()
... except StopIteration: return
... for item in seqiter:
... prev, curr, next = curr, next, item
... yield prev, curr, next
... yield curr, next, NULL
... ...
'\x00' 'a' 'b'
'a' 'b' 'c'
'b' 'c' 'd'
'c' 'd' 'e'
'd' 'e' 'f'
'e' 'f' '\x00' ...
'NULL' 0 1
0 1 2
1 2 3
2 3 'NULL'

If you want to assign back into myarray[i-1] etc, you'd still need to enumerate, but you could
combine with the above it was useful.

Regards,
Bengt Richter
 
A

Alex Martelli

Steven D'Aprano said:
I can't speak for others, but I've never come upon a situation where I
needed to access the element before and the element after the current one.

[thinks...] Wait, no, there was once, when I was writing a parser that
iterated over lines. I needed line continuations, so if the line ended
with a backslash, I needed to access the next line (and potentially the
line after that, and so forth indefinitely). I dealt with that by keeping
a cache of lines seen, adding to the cache if the line ended with a
backslash.

In Python, that would be an excellent example use case for a generator
able to "bunch up" input items into output items, e.g.:

def bunch_up(seq):
cache = []
for item in seq:
if item.endswith('\\\n'):
cache.append(item[:-2])
else:
yield ''.join(cache) + item
cache = []

if cache:
raise ValueError("extra continuations at end of sequence")

[[or whatever you wish to do instead of raising if the input sequence
anomalously ends with a ``to be continued'' line]].


Alex
 
J

Joseph Garvin

Steven said:
Python lists aren't linked lists? They are arrays.

[slaps head for the stupid typo]
That should have been a full stop, not question mark. Python lists are not
linked lists, period.
All the more inexcusable then, because it's even easier to find the
next/previous element in an array! ;)


Thanks all, Bas/Bangt's solutions are very elegant =)
 
S

Steve Holden

Steven said:


[snip example]

Well well, I'll be a monkey's uncle. You learn something new everyday.

Okay, I was wrong. There is no conflict between using j as a name and the
built-in j-as-syntax-for-complex-numbers, except for any potential
conflict in the programmers head.

That makes me feel a lot less guilty about all the times I wrote for j in
range loops :)
Yup, the "j" notation is recognised by the lexer, there's no built-in
object.

regards
Steve
 
P

Paul Rubin

Joseph Garvin said:
And this way I can keep referring to j instead of myarray, but I'm
still forced to use myarray[i-1] and myarray[i+1] to refer to the
previous and next elements. Being able to do j.prev, j.next seems more
intuitive.

Is there some other builtin somewhere that provides better
functionality that I'm missing?


You could use a generator comprehension (untested):

for prev,cur,next in ((myarray[i-1], myarray, myarray[i+1])
for i in xrange(1,len(myarray)-1)):
do_something_with (prev, cur, next)

Alternatively (untested):

elts = iter(myarray)
prev,cur,next = elts.next(),elts.next(),elts.next()
for next2 in elts:
do_something_with (prev, cur, next)
prev,cur,next = cur, next, next2

Of course these fail when there's less than 3 elements.
 
P

Paul Rubin

Paul Rubin said:
elts = iter(myarray)
prev,cur,next = elts.next(),elts.next(),elts.next()
for next2 in elts:
do_something_with (prev, cur, next)
prev,cur,next = cur, next, next2

Of course these fail when there's less than 3 elements.

Ehh, too clever for my own good. This fails with exactly 3 elements.
It was a "shortened" version of

elts = iter(myarray)
prev,cur,next = elts.next(),elts.next(),elts.next()
try:
while True:
do_something_with (prev, cur, next)
prev,cur,next = cur, next, elts.next()
except StopIteration: pass

which is messy, and which could get confused by do_something_with...
raising StopIteration for some reason.
 
P

Peter Otten

Bengt said:
...     seqiter = iter(seq)
...     prev = curr = NULL
...     try: next = seqiter.next()
...     except StopIteration: return
...     for item in seqiter:
...         prev, curr, next = curr, next, item
...         yield prev, curr, next
...     yield curr, next, NULL
...


Just a reminder:

"""
In a generator function, the return statement is not allowed to include
an expression_list[3]. In that context, a bare return indicates that the
generator is done and will cause StopIteration to be raised.
"""

try:
# may raise StopIteration
except StopIteration:
return

is the same as

try:
# may raise StopIteration
except StopIteration:
raise StopIteration

and may be simplified to

# may raise StopIteration

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top