views:

201

answers:

3

For arrays and lists in Python and Numpy are the following lines equivalent:

itemlist = []
for j in range(len(myarray)):
    item = myarray[j]
    itemlist.append(item)

and:

itemlist = []
for item in myarray:
    itemlist.append(item)

I'm interested in the order of itemlist. In a few examples that I have tried they are identical, but is it guaranteed? For example, I know that the foreach statement in C# doesn't guarantee order, and that I should be careful with it.

+8  A: 

Yes, it's entirely guaranteed. for item in myarray (where myarray is a sequence, which includes numpy's arrays, builtin lists, Python's array.arrays, etc etc), is in fact equivalent in Python to:

_aux = 0
while _aux < len(myarray):
  item = myarray[_aux]
  ...etc...

for some phantom variable _aux;-). Btw, both of your constructs are also equivalent to

itemlist = list(myarray)
Alex Martelli
Thanks! Yeah, I wasnt really trying to convert an array to a list, just exploring the order.
Chris
+5  A: 

Yes, the Python Language Reference guarantees this (emphasis is mine):

 for_stmt ::=  "for" target_list "in" expression_list ":" suite
               ["else" ":" suite]

"The suite is then executed once for each item provided by the iterator, in the order of ascending indices."

earl
+4  A: 

It is guaranteed for lists. I think the more relevant Python parallel to your C# example would be to iterate over the keys in a dictionary, which is NOT guaranteed to be in any order.

# Always prints 0-9 in order
a_list = [0,1,2,3,4,5,6,7,8,9]
for x in a_list:
    print x

# May or may not print 0-9 in order. Implementation dependent.
a_dict = {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}
for x in a_dict:
    print x

The for <element> in <iterable> structure only worries that the iterable supplies a next() function which returns something. There is no general guarantee that these elements get returned in any order over the domain of the for..in statement; lists are a special case.

Triptych
Thanks for highlighting that arrays have a notion of order, that it is implemented via the next() function. The for ... in ... structure takes advantage of next(), and so data with an intrinsic order (arrays) implements next() differently than data without an intrinsic order (such as dictionaries.)
Chris