Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Check Whether a List Is in Ascending Order

Python Check Whether a List Is in Ascending Order

Document Object Reference Manual Python3 Examples

We can write a function to check whether a list is in ascending order. The function iterates over every element in the list and checks whether the current element is less than or equal to the next one. If all elements satisfy this condition, the list is in ascending order.

Example

def

is_ascending

(

lst

)

:

   

for

i

in

range

(

len

(

lst

)

1

)

:

       

if

lst

[

i

]

>

;

lst

[

i +

1

]

:

           

return

False

   

return

True

Test cases

print

(

is_ascending

(

[

1

,

2

,

3

,

4

,

5

]

)

)

 

should return True

print

(

is_ascending

(

[

1

,

3

,

2

,

4

,

5

]

)

)

 

should return False

print

(

is_ascending

(

[

5

,

4

,

3

,

2

,

1

]

)

)

 

should return False

Code analysis:

Output result:

True
False
False

Document Object Reference Manual Python3 Examples