Solved

sorted() vs .sort()

TomBeckerOP

Which should I use? I got None from x = numbers.sort().

7

Comments 5

Join the discussion

Already have an account? Log in

  1. SophieDev Solutionreplies: 1

    - numbers.sort() sorts the list in place and returns None.
    - sorted(numbers) returns a new sorted list and leaves the original alone.

    So x = numbers.sort() sets x to None. Use one of:

    numbers.sort()
    x = sorted(numbers)
    4
    1. SamPrice

      There's a challenge called "sort() returns None" for a reason šŸ˜„

      5
      1. HenryWritesreplies: 1

        Both accept key= and reverse=True. E.g. sorted(names, key=len) sorts by length.

        0
        1. LucasVD

          And key=str.lower for case-insensitive sorting. Without it "Zebra" comes before "apple".

          0