Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list and sort query
#1
qwe=[1,2,3,9,8,7,6]

qwe.sort()
print(qwe)

# This give output [1, 2, 3, 6, 7, 8, 9]

BUT

print(qwe.sort())

# This give output None

Why please..
Reply
#2
Because it’s sorts in-place.

All functions/methods which don’t return anything explicitly return None.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
If you want a sorted version of the list without mutating the original, use sorted:

>>> l = [3, 1, 2]
>>> sorted(l)
[1, 2, 3]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to sort a list with duplicates and return their unique indices. Echoroom 3 3,455 Sep-23-2022, 07:53 AM
Last Post: deanhystad
  sorting a list using unicodes acending order, no loops, no sort(), using recursion lrn2codee 14 6,356 Jun-23-2021, 07:33 PM
Last Post: deanhystad
  how to sort a list without .sort() function letmecode 3 3,423 Dec-28-2020, 11:21 PM
Last Post: perfringo
  [split] Manual Sort without Sort function fulir16 2 3,159 Jun-02-2019, 06:13 AM
Last Post: perfringo
  Manual Sort without Sort function dtweaponx 26 48,937 Jun-01-2019, 06:02 PM
Last Post: SheeppOSU
  Query in list.clear (beginner) Shaswat 1 1,908 Apr-30-2019, 01:54 PM
Last Post: ichabod801
  sort a list alphabeticaly without changing the original list Holmen 5 4,196 Jan-27-2019, 01:49 PM
Last Post: Holmen
  bubble sort random list atux_null 7 7,883 Nov-03-2017, 07:28 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