You are currently viewing Difference Between sort() And sorted() In Python

Difference Between sort() And sorted() In Python

We’ll compare the Python’s list sort() and sorted() functions in this article. We’ll learn about these functions, such as what they are and how they differ programmatically and syntactically.

Python sort() and sorted() are used to sort the data in ascending or descending order. Their goals are the same but are used in different conditions.

sort()

The sort() function is connected to the Python list and by default, sorts the list’s contents in ascending order.

The code above sorts the names in ascending order within the list data. That was the most fundamental use of the list sort() function. We’ll see more examples where we’ll manipulate the function’s parameters.

Syntax

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

Here

reverse – Defaults to False. If reverse=True, the data will be sorted in descending order.

key – Defaults to None. We can specify a user-defined function to customize the sorting.

Examples

We’ll play around with these parameters and try with data having different data types to get a better understanding of this function.

Example 1 – List having different characters

Example 2 – List containing the different data types

In the following example, data is a list that contains tuples and we sorted the data in descending order.

What if we specify a similar name for the first values of the tuple?

The tuples were sorted based on the second value because sort() cannot perform sorting on similar values, or more specifically, how can data be sorted in ascending or descending order if they are all similar?

Example 3 – Using a user-defined function

We’ve written a function called sort_dict_by_price that takes a parameter item, which is our dictionary itself, and returns the values of the key 'price'.

This function was passed to the key parameter, which will sort the data based on the price in ascending order.

Output

Instead of explicitly defining the sort_dict_by_price function, we could have used the lambda function in the above code.

We changed the code above and passed the lambda function to the key. The expression lambda item: item['fruit'] is equivalent to the previous code’s sort_dict_by_price function.

Output

Example 4 – Sorting tuple data by specifying sorting criteria

In the following example, data is a list containing tuples and we sorted the data in descending order based on the first items of the tuple.

Output

sorted()

Python sorted() function is used to sort the iterable data. By default, this function sorts the data in ascending order.

We have nested tuple data stored in the variable tuple_data, used the sorted() function with our iterable as a parameter, and then printed the sorted data.

Output

The data were sorted based on the first item of the tuple tuple_data.

Syntax

sorted(iterable, key=None, reverse=False)

Here

iterable – Required. Any iterable data

key – defaults to None. To specify the sorting criteria.

reverse – Defaults to False. When set to True, the data will be sorted in descending order.

Examples

We have data of various data types that are all iterable; we’ll sort them using the sorted() function.

Output

Example 2 – Using key and reverse parameters

Output

Due to the use of the key parameter where we passed the custom function, the tuple was sorted based on the second item and data was sorted in descending order because the reverse was set to True.

Difference

sort()sorted()
Used to sort the Python List.Used to sort any iterable data such as ListTupleDictionary, and more.
Takes two parameters: key and reverse.Takes three parameters: iterablekey and reverse.
It is a List function(list.sort()) and can only work with Lists.It is a function to sort any data which can be iterated.
sort() modifies the original list.sorted() doesn’t modify the original data instead it returns the new modified data.

Conclusion

We’ve seen a comparison of the list sort() and sorted() functions. We’ve coded the examples to understand how these functions work. Both functions are used to sort data, but the sort() function only sorts Python lists, whereas the sorted() function sorts iterable data.

We’ve also seen the differences between the two in a table format.


🏆Other articles you might be interested in if you liked this one

Comparing the list reverse and reversed functions.

8 different ways to reverse a Python list.

NumPy argmax() and TensorFlow argmax() – Are they similar?

Execute your code dynamically using the exec() in Python.

Perform high-level file operations on files in Python.

Number your iterable data using the enumerate() in Python.

Understanding args and kwargs in function parameter in Python.


That’s all for now

Keep Coding✌✌