= ["MSFT", "AAPL", "GOOGL", "AMZN", "NFLX"]
symbols print(type(symbols))
print(symbols)
<class 'list'>
['MSFT', 'AAPL', 'GOOGL', 'AMZN', 'NFLX']
A sorting operation will arrange items in a specific order.
In Python, we can use the sorted
function to sort a list. To use the sorted
function, we pass in as the first parameter the list we want to sort.
In the case of simple lists, this is sufficient:
<class 'list'>
['MSFT', 'AAPL', 'GOOGL', 'AMZN', 'NFLX']
Notice, this is not a mutating operation, so if we want to use the results, we’ll need to store the sorted list in a variable for later:
['MSFT', 'AAPL', 'GOOGL', 'AMZN', 'NFLX']
['AAPL', 'AMZN', 'GOOGL', 'MSFT', 'NFLX']
['MSFT', 'AAPL', 'GOOGL', 'AMZN', 'NFLX']
print(symbols)
sorted_symbols = sorted(symbols) # STORING THE RESULT FOR LATER
print(sorted_symbols) # NOW ITS SORTED
['MSFT', 'AAPL', 'GOOGL', 'AMZN', 'NFLX']
['AAPL', 'AMZN', 'GOOGL', 'MSFT', 'NFLX']
There are some optional parameters we can use, for example, after consulting the function documentation, we see the reverse
parameter takes a boolean value to determine which order the list should be sorted in (ascending or descending):
['AAPL', 'AMZN', 'GOOGL', 'MSFT', 'NFLX']
We see that if we have a simple list, such as a list of numbers or list of strings, the sorted
function will know how to sort the items. It understands numeric order in which 2 is greater than 1, and it understands alphabetical order in which “b” is greater than “a”.
But it is not possible to compare dictionaries using greater than or less than operators:
So if we have a complex list, such as a list of dictionaries, we need to specify on the basis of which key we should sort on.
The key
parameter is used for this purpose. In practice, when specifying what key to sort on, we can use a lambda
statement, a custom function, or the itemgetter
function from the operator
module. All of the following approaches are equivalent:
# itemgetter function approach:
from operator import itemgetter
sorted(teams, key=itemgetter("name"))
[{'city': 'New York', 'name': 'Mets'},
{'city': 'New Haven', 'name': 'Ravens'},
{'city': 'Boston', 'name': 'Red Sox'},
{'city': 'New York', 'name': 'Yankees'}]