Python sorted() Function Explained
sorted() is a built-in Python function that returns a new list in sorted order.
It is useful when you want to sort values without changing the original data. Unlike list.sort(), sorted() also works with many iterable types, not just lists.
Quick answer
numbers = [3, 1, 2]
result = sorted(numbers)
print(result) # [1, 2, 3]
print(numbers) # [3, 1, 2]
sorted() returns a new sorted list and does not change the original object.
What sorted() does
sorted()is a built-in Python function.- It returns a new list in sorted order.
- It works with many iterable objects, not just lists.
- It does not modify the original data.
This means you can use it with lists, tuples, sets, strings, and other iterable values.
Basic syntax
sorted(iterable, key=None, reverse=False)
Parameters
iterable: the values to sortkey: an optional function that decides how items are comparedreverse: set toTrueto sort in descending order
In many cases, you only need:
sorted(my_values)
Simple example
Here is a basic example with numbers:
numbers = [5, 2, 8, 1]
sorted_numbers = sorted(numbers)
print(sorted_numbers)
print(numbers)
Output:
[1, 2, 5, 8]
[5, 2, 8, 1]
Notice the difference:
sorted_numbersis a new sorted listnumbersstays unchanged
If you want to sort a list in place instead, see how to sort a list in Python.
Sorting different iterable types
sorted() works with more than lists. The result is always a new list.
List
numbers = [3, 1, 2]
print(sorted(numbers))
Output:
[1, 2, 3]
Tuple
values = (3, 1, 2)
print(sorted(values))
print(type(sorted(values)))
Output:
[1, 2, 3]
<class 'list'>
Set
values = {3, 1, 2}
print(sorted(values))
Output:
[1, 2, 3]
String
text = "python"
print(sorted(text))
Output:
['h', 'n', 'o', 'p', 't', 'y']
Dictionary
When you pass a dictionary to sorted(), Python sorts the dictionary keys.
person = {"name": "Ana", "age": 20, "city": "Lima"}
print(sorted(person))
Output:
['age', 'city', 'name']
Using reverse=True
Use reverse=True when you want descending order instead of ascending order.
numbers = [3, 1, 2]
print(sorted(numbers))
print(sorted(numbers, reverse=True))
Output:
[1, 2, 3]
[3, 2, 1]
This also works with strings:
words = ["apple", "banana", "cherry"]
print(sorted(words))
print(sorted(words, reverse=True))
Output:
['apple', 'banana', 'cherry']
['cherry', 'banana', 'apple']
Using key= for custom sorting
The key= argument lets you control how values are sorted.
Python applies the key function to each item, then sorts based on the result.
A beginner-friendly example is sorting words by length:
words = ["pear", "fig", "banana", "kiwi"]
result = sorted(words, key=len)
print(result)
Output:
['fig', 'pear', 'kiwi', 'banana']
Here, len is used as the key function, so the words are sorted by length.
Another common example is sorting text without caring about uppercase and lowercase letters:
words = ["Banana", "apple", "Cherry"]
result = sorted(words, key=str.lower)
print(result)
Output:
['apple', 'Banana', 'Cherry']
You can learn more about len() in Python len() function explained.
sorted() vs list.sort()
The main difference is this:
sorted()returns a new listlist.sort()changes the original list
Also:
sorted()works with many iterableslist.sort()only works on lists
Use sorted() when:
- you want to keep the original data unchanged
- your data is not a list
For a full side-by-side comparison, see Python sorted() vs list.sort() explained.
What can cause errors
A few common problems happen when using sorted().
Mixing values that cannot be compared
This can raise a TypeError:
values = [1, "2", 3]
print(sorted(values))
Python cannot sort integers and strings together in the normal way.
Using the wrong key function
If your key function does something invalid for the item type, you may get an error.
Example:
numbers = [10, 2, 30]
print(sorted(numbers, key=len))
This fails because len() does not work on integers.
Expecting the original list to change
This is not a syntax error, but it is a very common beginner mistake:
numbers = [3, 1, 2]
sorted(numbers)
print(numbers)
Output:
[3, 1, 2]
If you do not save the result, the original list stays the same.
Common mistakes
- Assuming
sorted()changes the original list - Trying to sort mixed types like integers and strings together
- Forgetting that
sorted()always returns a list - Using
key=incorrectly - Calling
sort()on a non-list object instead of usingsorted()
These quick checks can help while debugging:
print(sorted([3, 1, 2]))
print(type(sorted((3, 1, 2))))
print(sorted(['Banana', 'apple'], key=str.lower))
print(sorted([3, 1, 2], reverse=True))
If Python raises an error because values cannot be compared, that is often a type problem in your data. You may also want to review common list behavior in Python lists explained for beginners.
FAQ
Does sorted() change the original list?
No. It returns a new sorted list and leaves the original list unchanged.
What does sorted() return?
It always returns a new list, even if you sort a tuple, set, or string.
When should I use sorted() instead of list.sort()?
Use sorted() when you want a new list or when your data is not a list.
What does key= do in sorted()?
It tells Python what value to use when comparing each item during sorting.
Why does sorted() give a TypeError?
Usually because the items cannot be compared directly, such as mixing strings and integers.