Built in function Grammar/Environments List

[Python] How to Sort List [sort method, sorted function]

This article shows how to sort list.

Contents

  • sort method : sort the original list
  • sorted function : sort list and generate new list
  • Sort numerical values in descending order
  • Sort numerical values by absolute value
  • Sort strings

sponsored link

sort method : sort the original list

By using the sort method, the list is sorted.

The original list is rewritten.

List.sort( key=None, reverse=False )

"Key" and "Reverse" are optional specification and can be omitted.

"Key" and "Reverse" are explained later in this article.

Here is an example code.

num_list = [41, 52, 15, 38, 22]

num_list.sort()
print(num_list)
# [15, 22, 38, 41, 52]

sponsored link

sorted function : sort list and generate new list

By using the sorted function, the list is sorted.

A new list is returned after sorting, preserving the list before sorting.

New List = sorted( List, key=None, reverse=False )

Here is an example code.

num_list = [41, 52, 15, 38, 22]

new_num_list = sorted(num_list)

print(num_list)
# [41, 52, 15, 38, 22]
print(new_num_list)
# [15, 22, 38, 41, 52]

sponsored link

Sort numerical values in descending order

You can sort the list in descending order by setting "reverse=True".

◆sort method

List.sort( key=None, reverse=True )

◆sorted function

New List = sorted( List, key=None, reverse=True )

Here is a example code.

◆sort method

num_list = [41, 52, 15, 38, 22]

num_list.sort(reverse=True)
print(num_list)
# [52, 41, 38, 22, 15]

◆sorted function

num_list = [41, 52, 15, 38, 22]

new_num_list = sorted(num_list, reverse=True)

print(num_list)
# [41, 52, 15, 38, 22]
print(new_num_list)
# [52, 41, 38, 22, 15]

sponsored link

Sort numerical values by absolute value

You can sort the list by absolute value by setting "key=abs".

The function abs() is a built-in function that returns absolute values.

◆sort method

List.sort( key=abs, reverse=False )

◆sorted function

New List = sorted( List, key=abs, reverse=False )

Here is an example code.

◆sort method

num_list = [41, -52, 15, 38, -22]

num_list.sort(key=abs)
print(num_list)
# [15, -22, 38, 41, -52]

◆sorted function

num_list = [41, -52, 15, 38, -22]

new_num_list = sorted(num_list, key=abs)

print(num_list)
# [41, 52, 15, 38, 22]
print(new_num_list)
# [15, -22, 38, 41, -52]

sponsored link

Sort strings

Lists with string elements can also be sorted using sort method and sorted function.

Sort strings in alphabetical order

You can sort a list in alphabetical order by simply specifying a list with string elements.

Here is an example code.

◆sort method

fruits = ["Banana", "Orange", "Apple"]

fruits.sort()
print(fruits)
# ['Apple', 'Banana', 'Orange']

◆sorted function

fruits = ["Banana", "Orange", "Apple"]

sort_fruits = sorted(fruits)
print(sort_fruits)
# ['Apple', 'Banana', 'Orange']

Sort numerical values of string type

Example of sorting zero-filled numerical values of string type.

◆sort method

num_list = ['130', '002', '017']

num_list.sort()
print(num_list)
# ['002', '017', '130']

◆sorted function

num_list = ['130', '002', '017']

new_num_list = sorted(num_list)
print(new_num_list)
# ['002', '017', '130']

Example of sorting non zero-filled numerical values of string type.

You can sort list by converting string to integer type.

Here is an example code.

◆sort method

num_list = ['130', '2', '17']

num_list.sort()
print(num_list)
# ['130', '17', '2']

◆sorted function

num_list = ['130', '2', '17']

num_list.sort(key=int)
print(num_list)
# ['2', '17', '130']

Sort by string length

You can sort the list by string length by setting "key=len".

The function len() is a built-in function that returns string length.

◆sort method

List.sort( key=len, reverse=False )

New List = sorted( List, key=abs, reverse=False )

Here is an example code.

◆sort method

fruits = ["Banana", "Plum", "Apple"]

fruits.sort(key=len)
print(fruits)
# ['Plum', 'Apple', 'Banana']

◆sorted function

fruits = ["Banana", "Plum", "Apple"]

new_fluits = sorted(fruits, key=len)
print(new_fluits)
# ['Plum', 'Apple', 'Banana']

sponsored link

-Built in function, Grammar/Environments, List
-, ,