note.nkmk.me

Sort a list, string, tuple in Python (sort, sorted)

Posted: 2020-02-02 / Tags: Python, List, String

In Python, there are two ways, sort() and sorted(), to sort lists (list) in ascending or descending order. If you want to sort strings (str) or tuples (tuple), use sorted().

This post describes the following contents.

  • List type method sort() sorts the original list
  • Built-in function sorted() returns a sorted list
  • How to sort strings and tuples

If you want to reverse or shuffle elements randomly, see the following posts.

Sponsored Link

List type method sort() sorts the original list

sort() is a list type method.

sort() is a destructive process that sort the original list in place.

org_list = [3, 1, 4, 5, 2]

org_list.sort()
print(org_list)
# [1, 2, 3, 4, 5]

Note that reverse() returns None.

print(org_list.sort())
# None

By default, the list is sorted in ascending order. If you want to sort in descending order, set the parameter reverse to True.

org_list.sort(reverse=True)
print(org_list)
# [5, 4, 3, 2, 1]

Built-in function sorted() returns a sorted list

sorted() is a built-in function.

Specifying a list to sorted() returns a sorted list. The original list remains unchanged.

org_list = [3, 1, 4, 5, 2]

new_list = sorted(org_list)
print(org_list)
print(new_list)
# [3, 1, 4, 5, 2]
# [1, 2, 3, 4, 5]

Like sort(), by default, the list is sorted in ascending order. If you want to sort in descending order, set the parameter reverse to True.

new_list_reverse = sorted(org_list, reverse=True)
print(org_list)
print(new_list_reverse)
# [3, 1, 4, 5, 2]
# [5, 4, 3, 2, 1]
Sponsored Link

How to sort strings and tuples

Since strings and tuples are immutable, there is no sort() method that update the original object.

On the other hand, you can specify not only lists but also strings and tuples to the sorted() function that creates a new sorted list. Since sorted() returns a list, it must be converted to a string or tuple.

sort strings

Passing a string to sorted() returns a list containing the sorted characters as elements.

org_str = 'cebad'

new_str_list = sorted(org_str)
print(org_str)
print(new_str_list)
# cebad
# ['a', 'b', 'c', 'd', 'e']

Use the join() method to concatenate a list of characters into a single string.

new_str = ''.join(new_str_list)
print(new_str)
# abcde

You can write in one line. If you want to sort in descending order, set the argument reverse to True.

new_str = ''.join(sorted(org_str))
print(new_str)
# abcde

new_str_reverse = ''.join(sorted(org_str, reverse=True))
print(new_str_reverse)
# edcba

The order of the characters is determined by their Unicode code point.

Sort tuples

Sorting tuples is the same as for strings. Passing a tuple to sorted() returns a sorted list.

org_tuple = (3, 1, 4, 5, 2)

new_tuple_list = sorted(org_tuple)
print(org_tuple)
print(new_tuple_list)
# (3, 1, 4, 5, 2)
# [1, 2, 3, 4, 5]

To convert a list to a tuple, use tuple().

new_tuple = tuple(new_tuple_list)
print(new_tuple)
# (1, 2, 3, 4, 5)

You can write in one line. If you want to sort in descending order, set the argument reverse to True.

new_tuple = tuple(sorted(new_tuple_list))
print(new_tuple)
# (1, 2, 3, 4, 5)

new_tuple_reverse = tuple(sorted(new_tuple_list, reverse=True))
print(new_tuple_reverse)
# (5, 4, 3, 2, 1)
Sponsored Link
Share

Related Categories

Related Posts