Python Forum
list sort() function bring backs None
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list sort() function bring backs None
#1
i have a program that gives back a random list of numbers which i then sort using .sort() , but then this happened:

    list1 = [1,1,1,1,1]
    list2 = list1.sort[]
    print(list2)
output

None
why doesn't it just return

[1,1,1,1,1]

and what kind i do to bypass that problem?
Reply
#2
list1 = [1,1,1,1,1]
list2 = list1
list2.sort()
print(list2)
Output:
[1, 1, 1, 1, 1]
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
That is because the list sort method sorts in place and returns None
list1 = [3, 4, 1, 2]
list2 = list1.sort()
print(list1)
print(list2)
Output:
[1, 2, 3, 4] None
Use sorted if you want to get a sperate sorted version
list1 = [3, 4, 1, 2]
list2 = sorted(list1)
print(list1)
print(list2)
Output:
[3, 4, 1, 2] [1, 2, 3, 4]


@menator01 Your code has two variables list1 & list2 that point to the same list object
list1 = [3, 4, 1, 2]
list2 = list1
list2.sort()
print(list1)
print(list2)
Output:
[1, 2, 3, 4] [1, 2, 3, 4]
Larz60+ and BashBedlam like this post
Reply
#4
thank you
Reply
#5
@newb

You really need to do more research into these simple questions. It is easy to google "Why does sort return None?", but I prefer reading the Python documentation because I always learn more. This talks about all the things you can do with lists, including sorting.
https://docs.python.org/3/library/stdtyp...#list.sort

That references a nice article all about sorting.
https://docs.python.org/3/howto/sorting....rtinghowto

You post a question here and you get your answer. Eventually. When you search the documentation you get the answer faster, and learn more that is related to your question.
ndc85430 likes this post
Reply
#6
Thank you, I'll check out the ressources you just gave me before asking question here in the future but trust me i search the web before asking question, i just don't seem to be good at it
Reply
#7
Don't apologize, at one point or another we've all been there, and that is what this forum is all about!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list.sort() returning None SmallCoder14 8 406 Mar-19-2024, 09:49 PM
Last Post: SmallCoder14
  Sort a list of dictionaries by the only dictionary key Calab 1 452 Oct-27-2023, 03:03 PM
Last Post: buran
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,277 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  [solved] Sort list paul18fr 5 2,803 Aug-18-2021, 06:34 AM
Last Post: naughtyCat
  Sort Function: <' not supported between instances of 'float' and 'tuple' quest 2 7,971 Apr-30-2021, 07:37 PM
Last Post: quest
  Sort List of Lists by Column Nju 1 10,063 Apr-13-2021, 11:59 PM
Last Post: bowlofred
  Combine Two Recursive Functions To Create One Recursive Selection Sort Function Jeremy7 12 7,195 Jan-17-2021, 03:02 AM
Last Post: Jeremy7
  How to sort os.walk list? Denial 6 11,293 Oct-10-2020, 05:28 AM
Last Post: Denial
  Cannot bring up anaconda in Docker image ErnestTBass 1 1,765 Aug-15-2020, 04:12 AM
Last Post: ndc85430
  I created a function that generate a list but the list is empty in a new .py file mrhopeedu 2 2,247 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu

Forum Jump:

User Panel Messages

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