Sort a list, string, tuple in Python [sort, sorted]
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
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.
Note that reverse[] returns None.
By default, the list is sorted in ascending order. If you want to sort in descending order, set the parameter reverse to True.
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.
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.
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.
Use the join[] method to concatenate a list of characters into a single string.
- Concatenate strings in Python [+ operator, join, etc.]
You can write in one line. If you want to sort in descending order, set the argument reverse to True.
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.
To convert a list to a tuple, use tuple[].
- Convert lists and tuples to each other in Python
You can write in one line. If you want to sort in descending order, set the argument reverse to True.
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.]