My goal is to have python code allowing to detect if a list is sorted or not.
I would like to understand why the following code return True
instead of my expected guess False
l = [1, 2, 3, 4, 1, 6, 7, 8, 7]
all(l[i] <= l[i+1] for i in xrange(len(l)-1)) # return "True"
Notes:
- I'm using python 2.6.4 inside iPython 0.10
- I use very huge list so I'd prefer to avoid
solution of type
l == l.sort()
In the way to understand this I already read (and test) info from the main two following post:
EDIT: OK apparently the problem appear inside iPython ONLY, not when using only python command line!
iPython 0.10
In [93]: l = [1, 2, 3, 4, 1, 6, 7, 8, 7]
In [94]: all(l[i] <= l[i+1] for i in xrange(len(l)-1))
Out[94]: True
python 2.6.4
>>> l = [1, 2, 3, 4, 1, 6, 7, 8, 7]
>>> all(l[i] <= l[i+1] for i in xrange(len(l)-1))
False