![]() ![]() Output: Code language: Python ( python ) Using lambda expression # define a sort key def sort_key (company): return companyĬompanies.sort(key=sort_key, reverse= True) Note that you just pass the function name without the parentheses to the sort() method: companies = [( 'Google', 2019, 134.81), The sort() method will use the value returned by the sort_key() function for the comparisons. Second, pass the sort_key function to the sort() method: companies.sort(key=sort_key, reverse= True) Code language: Python ( python ) And the company references the revenue like 134.81 in this case. Note that the company is a tuple e.g., ('Google', 2019, 134.81). This sort_key() function accepts a tuple called company and returns the third element. To define a sort key, you create a function that accepts a tuple and returns the element that you want to sort by: def sort_key (company): return company Code language: Python ( python ) To do it:įirst, specify a sort key and pass it to the sort() method. ( 'Facebook', 2019, 70.7)] Code language: Python ( python )Īnd you want to sort the companies list by revenue from highest to lowest. Suppose that you have a list of tuples like this: companies = [( 'Google', 2019, 134.81), Output: Code language: Python ( python ) 3) Using the Python List sort() method to sort a list of tuples To sort numbers from the largest to smallest, you use the sort(reverse=True) like this: scores = ![]() Print(scores) Code language: Python ( python ) The following example uses the sort() method to sort numbers in the scores list from smallest to largest: scores = If a list contains numbers, the sort() method sorts the numbers from smallest to largest. Output: Code language: Python ( python ) 2) Using the Python List sort() method to sort a list of numbers Output: Code language: Python ( python )Īnd the following example uses the sort() method with the reverse=True argument to sort the elements in the guests list in the reverse alphabetical order: guests = Print(guests) Code language: Python ( python ) The following example uses the sort() method to sort the elements in the guests list alphabetically: guests = If a list contains strings, the sort() method sorts the string elements alphabetically. 1) Using the Python List sort() method to sort a list of strings Let’s take some examples of using the sort() method. To sort elements from higher to lower, you pass the reverse=True argument to the sort() method like this: list.sort(reverse= True) Code language: Python ( python ) Python List sort() method examples In other words, it places the lower elements before the higher ones. It means that the sort() method modifies the order of elements in the list.īy default, the sort() method sorts the elements of a list using the less-than operator ( <). The sort() method sorts the original list in place. To sort a list, you use the sort() method: list.sort() Code language: Python ( python ) Introduction to the Python List sort() method Summary: in this tutorial, you’ll learn how to use the Python List sort() method to sort a list. ![]()
0 Comments
Leave a Reply. |