Home » Python

Python List sort() Method with Example

Python List sort() Method: Here, we are going to learn how to sort the list elements in ascending and descending order using sort() method in Python?
Submitted by IncludeHelp, on December 15, 2019

List sort() Method

sort() method is used to sort the list elements in the ascending and descending order, the method is called with this list (whose elements to be sorted) and accept some optional parameters (explained below under parameters), the method does not return any value, it sorts this list.

Syntax:

    List_name.sort(reverse=True|False, key=function)

Parameter(s):

  • reverse=True|False – It is an optional parameter, it's default value is False which sorts list elements in ascending order, if we set True, it sorts the list elements in descending order.
  • key=function – It is also an optional parameter, it can be used to specify the sorting criteria.

Return value:

The return type of this method is <class 'NoneType'>, it returns nothing.

Example 1: Sorting list without specifying any parameter

# Python List sort() Method with Example

# declaring the list
cars = ["BMW", "Porsche", "Audi", "Lexus", "Audi"]

# printing the list
print("cars before sort operation...")
print("cars: ", cars)

# sorting the elements
cars.sort() # sorts in ascending order

# printing the list
print("cars after sort operation...")
print("cars: ", cars)

Output

cars before sort operation...
cars:  ['BMW', 'Porsche', 'Audi', 'Lexus', 'Audi']
cars after sort operation...
cars:  ['Audi', 'Audi', 'BMW', 'Lexus', 'Porsche']

Example 2: Sorting list with specifying the first parameter

# Python List sort() Method with Example

# declaring the list
cars = ["BMW", "Porsche", "Audi", "Lexus", "Audi"]

# printing the list
print("cars before sort operation...")
print("cars: ", cars)

# sorting the elements specifying reverse=True
cars.sort(reverse=True) 

# printing the list
print("cars list elements in descending order...")
print("cars: ", cars)

# sorting the elements specifying reverse=False
cars.sort(reverse=False) 

# printing the list
print("cars list elements in ascending order...")
print("cars: ", cars)

Output

cars before sort operation...
cars:  ['BMW', 'Porsche', 'Audi', 'Lexus', 'Audi']
cars list elements in descending order...
cars:  ['Porsche', 'Lexus', 'BMW', 'Audi', 'Audi']
cars list elements in ascending order...
cars:  ['Audi', 'Audi', 'BMW', 'Lexus', 'Porsche']

Example 3: Sorting list with specifying the both parameters

# Python List sort() Method with Example

# defining a function that will return length
def getLen(e):
    return len(e)
    
# declaring the list
cars = ["BMW", "Porsche", "Audi", "Lexus", "Audi"]

# printing the list
print("cars before sort operation...")
print("cars: ", cars)

# sorting the elements specifying reverse=True
cars.sort(reverse=True,key=getLen) 

# printing the list
print("cars list elements in descending order based on length...")
print("cars: ", cars)

# sorting the elements specifying reverse=False
cars.sort(reverse=False,key=getLen) 

# printing the list
print("cars list elements in ascending order based on length...")
print("cars: ", cars)

Output

cars before sort operation...
cars:  ['BMW', 'Porsche', 'Audi', 'Lexus', 'Audi']
cars list elements in descending order based on length...
cars:  ['Porsche', 'Lexus', 'Audi', 'Audi', 'BMW']
cars list elements in ascending order based on length...
cars:  ['BMW', 'Audi', 'Audi', 'Lexus', 'Porsche']






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.