Python List sort()

In this tutorial, we will see about Python List‘s sort method.Python List sort method is used to sort the list either in ascending or descending order.

Python List sort syntax

here list1 is object of list.As you can see key and reverse are optional parameters.

Python List sort example

You can simply call remove method to delete an element from the list.
Let’s understand this with the help of simple example.

listOfNames=[‘Sam’,’Mary’,’Martin’,’John’] print("listOfNames before sorting:",listOfNames)
listOfNames.sort()
print("listOfNames after sorting in ascending order:",listOfNames)
listOfNames.sort(reverse=True)
print("listOfNames after sorting in descending order:",listOfNames)

Output:

​listOfNames before sorting: [‘Sam’, ‘Mary’, ‘Martin’, ‘John’] listOfNames after sorting in ascending order: [‘John’, ‘Martin’, ‘Mary’, ‘Sam’] listOfNames after sorting in descending order: [‘Sam’, ‘Mary’, ‘Martin’, ‘John’]

As you can see here, we have sorted in ascending and descending order.
You can also sort list on the basis of key function. Let’s understand with the help of example.

listOfNames=[‘Sam’,’Mary’,’Martin’,’John’] print("listOfNames before sorting:",listOfNames)
listOfNames.sort(key=len)
print("listOfNames after sorting on the basis of len:",listOfNames)

Output:

listOfNames before sorting: [‘Sam’, ‘Mary’, ‘Martin’, ‘John’] listOfNames after sorting on the basis of len: [‘Sam’, ‘Mary’, ‘John’, ‘Martin’]

What if list has mixed data type

If you have different data type in the list and they are not comparable then sort method will raise TypeError.

listOfNames=[1,’two’,3,’four’] print("listOfNames before sorting:",listOfNames)
listOfNames.sort()
print("listOfNames after sorting:",listOfNames)

Output:

listOfNames before sorting: [1, ‘two’, 3, ‘four’] —————————————————————————
TypeError Traceback (most recent call last)
in ()
1 listOfNames=[1,’two’,3,’four’] 2 print(“listOfNames before sorting:”,listOfNames)
—-> 3 listOfNames.sort()
4 print(“listOfNames after sorting:”,listOfNames)
5

TypeError: ‘<' not supported between instances of 'str' and 'int'

That’s all about Python List sort method.

Was this post helpful?


import_contacts

You may also like:

Related Posts

  • 24 May

    Replace Comma with Space in List in Python

    Table of ContentsHow to replace comma with space in list in PythonUsing the replace() function to replace comma with space in list in PythonUsing the re.sub() function to replace comma with space in list in PythonUsing the split() and join() functions to replace comma with space in list in Python.ConclusionWas this post helpful? In this […]

  • 19 May

    Compare list elements with each other in Python

    Table of ContentsCompare all elements in list PythonWays to compare list elements with each other in PythonUsing the for loop to compare all elements in list PythonUsing the itertools.combinations() function to compare all elements in list PythonConclusionWas this post helpful? In this post, we will see how to compare list elements with each other in […]

  • 18 May

    How to Deep Copy a List in Python

    Table of ContentsWhat is a list in Python?What are shallow copy and deep copy operations in Python?Shallow CopyDeep CopyHow to deep copy a list in Python?Using the deepcopy() function from the copy module to deep copy a list in Python.Using a user-defined function to deep copy a list in Python.Using list comprehension to deep copy […]

  • 18 May

    Convert Generator to List in Python

    Table of ContentsLists and Generators in PythonWays to convert generator to list in PythonUsing the list() function to convert generator to list in PythonUsing the unpack operator * to convert generator to list in PythonUsing the list comprehension to convert generator to list in PythonUsing the append() function to convert generator to list in PythonConclusionWas […]

  • Create a list from 1 to 100 in Python
    03 May

    Create a List from 1 to 100 in Python

    Table of ContentsWays to create a list from 1 to 100 in PythonUsing the range() function to create a list from 1 to 100 in PythonUsing the numpy.arange() function to create a list from 1 to 100 in PythonUsing the for loop with range() to create a list from 1 to 100 in PythonConclusionWas this […]

  • 07 January

    How to compare lists in Python

    Table of ContentsPython compare listsHow to compare lists in PythonUsing the sort() function and == operatorUsing the map() and reduce() functionsUsing the set() function and == operatorUsing the for loopUsing the set() function and - operatorUsing the collections.Counter() class and == operatorUsing the zip(), sum(), and len() functionsUsing the cmp() functionConclusionWas this post helpful? In […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.