Python Forum
defining a function to see if a list is sorted
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
defining a function to see if a list is sorted
#11
A note on looping through consecutive items of a list:

for first, second in zip(the_list, the_list[1:]):
    if first > second:
        return False
return True
That does give a performance hit because you are creating a new list. For small lists that shouldn't be a big issue. If large lists are a possibility you can use islice from itertools.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
RE: defining a function to see if a list is sorted - by ichabod801 - Nov-29-2018, 02:35 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  A function that checks if the list is sorted pooyan89 16 18,015 Apr-22-2024, 09:14 AM
Last Post: DeaD_EyE
  How to Sorted and display the Subclasses of BaseException Fernando_7obink 9 4,046 Feb-10-2021, 12:04 PM
Last Post: buran
  Defining x and y axes in Spectrogram Function amy5678 3 2,441 Nov-29-2020, 01:42 PM
Last Post: jefsummers
  Functions returns content of dictionary as sorted list kyletremblay15 1 2,174 Nov-21-2019, 10:06 PM
Last Post: ichabod801
  python 3.4.3 Question a defining function student8 1 4,433 Oct-02-2017, 07:59 PM
Last Post: nilamo
  sorted list not displaying Crackity 6 5,322 Jul-18-2017, 12:50 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020