#๐Ÿ”’ Sort method for list

16 messages ยท Page 1 of 1 (latest)

ocean pivot
#

I am dumbfounded as to why this prints None:

print([1,2].sort())

No I have not made a separate function called print or anything, this is the entire script (I am playing around learning sorting algorithms):

import subprocess
subprocess.run('clear')

array = [1, 3, 3, 0, 5, 2, 6]

def selectionsort(arr : list):
    for start_idx in range(len(arr)):
        min_element = arr[start_idx]
        for element in arr[start_idx:]:
            if element < min_element:
                min_element = element
        min_idx = arr[start_idx:].index(min_element) + start_idx

        arr[min_idx], arr[start_idx] = arr[start_idx], arr[min_idx]

    return arr
    
print(selectionsort(array))
print([1, 2].sort())

If my selectionsort is wrong or ineffective I don't want any help with that, I just want to figure out why this prints None for now.

agile sageBOT
#

@ocean pivot

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

mortal blade
#

sort method does an in place sort and returns None

#

You probably want sorted

tawny thistle
#

was js gonna type that

ocean pivot
#

Okay thanks!

#

That make the entire thing work

toxic slate
#

!e

x = [3, 2, 1]

y = sorted(x)
print(y)

x.sort()
print(x)

print(x.sort())

tawny thistle
#

you could store the array in a variable and sort it, then use it.

agile sageBOT
toxic slate
#

There you go

tawny thistle
#

@toxic slate oh damn, i did not know we could eval code with bot..

toxic slate
#

.sort() sorts the list without returning a new one, while sorted() creates a new sorted list

ocean pivot
#

!close

agile sageBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.