Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is 'for x in array' always result in sorted x? [Python/NumPy]

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.

like image 428
Chris Avatar asked Aug 06 '09 02:08

Chris


3 Answers

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.

like image 119
Kenan Banks Avatar answered Sep 30 '22 13:09

Kenan Banks


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)
like image 41
Alex Martelli Avatar answered Sep 30 '22 14:09

Alex Martelli


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."

like image 37
earl Avatar answered Sep 30 '22 15:09

earl