Sort list in list Python

Sort a list, string, tuple in Python [sort, sorted]

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

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 article 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 articles.

  • Reverse a list, string, tuple in Python [reverse, reversed]
  • Shuffle a list, string, tuple in Python [random.shuffle, sample]

sort[] and sorted[] have the key parameter which is not described here. See the following article for examples using the key parameter.

  • Sort a list of dictionaries by the value of the specific key in Python
Sponsored Link

List type method sort[] sorts the original list

sort[] is a list type method.

  • Built-in Types - Lists - sort[] Python 3.8.1 documentation

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]
source: sort_sorted.py

Note that reverse[] returns None.

print[org_list.sort[]] # None
source: sort_sorted.py

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]
source: sort_sorted.py

Built-in function sorted[] returns a sorted list

sorted[] is a built-in function.

  • Built-in Functions Python 3.8.1 documentation

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]
source: sort_sorted.py

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]
source: sort_sorted.py
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']
source: sort_sorted.py

Use the join[] method to concatenate a list of characters into a single string.

  • Concatenate strings in Python [+ operator, join, etc.]
new_str = ''.join[new_str_list] print[new_str] # abcde
source: sort_sorted.py

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
source: sort_sorted.py

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

  • Compare strings in Python [exact match, partial match, etc.]

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]
source: sort_sorted.py

To convert a list to a tuple, use tuple[].

  • Convert lists and tuples to each other in Python
new_tuple = tuple[new_tuple_list] print[new_tuple] # [1, 2, 3, 4, 5]
source: sort_sorted.py

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]
source: sort_sorted.py
Sponsored Link
Share
Tweet

Related Categories

  • Python
  • List
  • String

Related Articles

  • Convert a list of strings and a list of numbers to each other in Python
  • Extract and replace elements that meet the conditions of a list of strings in Python
  • How to slice a list, string, tuple in Python
  • Reverse a list, string, tuple in Python [reverse, reversed]
  • Convert 1D array to 2D array in Python [numpy.ndarray, list]
  • Extract the file, dir, extension name from a path string in Python
  • List comprehensions in Python
  • How to flatten a list of lists in Python
  • Convert numpy.ndarray and list to each other
  • Split strings in Python [delimiter, line break, regex, etc.]
  • in operator in Python [for list, string, dictionary, etc.]
  • zip[] in Python: Get elements from multiple lists
  • Convert pandas.DataFrame, Series and list to each other
  • Count elements from a list with collections.Counter in Python
  • Concatenate strings in Python [+ operator, join, etc.]

Video liên quan

Chủ Đề