tags:

views:

153

answers:

2

I've been mucking around a bit with Python, and I've gathered that it's usually better (or 'pythonic') to use

for x in SomeArray:

rather than the more C-style

for i in range(0, len(SomeArray)):

I do see the benefits in this, mainly cleaner code, and the ability to use the nice map() and related functions. However, I am quite often faced with the situation where I would like to simultaneously access elements of varying offsets in the array. For example, I might want to add the current element to the element two steps behind it. Is there a way to do this without resorting to explicit indices?

+9  A: 

The way to do this in Python is:

for i, x in enumerate(SomeArray):
    print i, x

The enumerate generator produces a sequence of 2-tuples, each containing the array index and the element.

Greg Hewgill
But how would I access previous / subsequent elements in one iteration of the loop?
int3
Inside the loop, you can access SomeArray[i-1] or SomeArray[i+1].
Greg Hewgill
`SomeArray[i-1]`? But you might want to explain what you're actually doing
SilentGhost
oh.. I see the link now. thanks!
int3
Since you have the index of the current element in `i`, you can access elements around it by adding or subtracting some offset from `i`, and accessing the element at that position.
Greg Hewgill
Love enumerate().
hughdbrown
+3  A: 

List indexing and zip() are your friends.

Here's my answer for your more specific question:

I might want to add the current element to the element two steps behind it. Is there a way to do this without resorting to explicit indices?

arr = range(10)
[i+j for i,j in zip(arr[:-2], arr[2:])]

You can also use the module numpy if you intend to work on numerical arrays. For example, the above code can be more elegantly written as:

import numpy
narr = numpy.arange(10)
narr[:-2] + narr[2:]

Adding the nth element to the (n-2)th element is equivalent to adding the mth element to the (m+2) element (for the mathematically inclined, we performed the substitution n->m+2). The range of n is [2, len(arr)) and the range of m is [0, len(arr)-2). Note the brackets and parenthesis. The elements from 0 to len(arr)-3 (you exclude the last two elements) is indexed as [:-2] while elements from 2 to len(arr)-1 (you exclude the first two elements) is indexed as [2:].

I assume that you already know list comprehensions.

ianalis
yep I do know list comprehensions. haven't really used them though. zip() looks like another nice solution, thanks!
int3