Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Sorted Fundamentals | Higher-Order Functions and Lambdas
Functional Programming Concepts in Python
Sektion 2. Kapitel 4
single

single

Sorted Fundamentals

Stryg for at vise menuen

Sorting data is a common operation when working with lists in Python. The sorted function is a built-in higher-order function that allows you to sort any iterable and returns a new sorted list, leaving the original data unchanged. You can use sorted with or without custom sorting logic. By default, sorted arranges elements in ascending order, but you can control the sort order and criteria using its optional parameters.

The most important parameters for sorted are:

  • iterable: the data you want to sort;
  • key: an optional function that determines the value to sort by for each element;
  • reverse: a Boolean value that, if set to True, sorts the data in descending order.

Suppose you have a list of numbers and want to sort them in ascending order. You can simply call sorted(numbers). If you want to sort the numbers in descending order, you can pass reverse=True.

12345
numbers = [9, 5, 2, 1] # Sorting in reverse order sorted_desc = sorted(numbers, reverse=True) print(sorted_desc)

You can also use the key parameter to sort more complex data structures, such as lists of tuples or dictionaries, by specifying a function that extracts the comparison value from each element.

12345
words = ["apple", "kiwi", "banana"] # Sorting by the length of each string sorted_words = sorted(words, key=len) print(sorted_words)

You can pass any function to the key parameter, including a lambda function or a previously defined function. This makes sorted a higher-order function, as it accepts another function as an argument. Sorting is not limited to numbers or strings; you can sort complex objects as long as you provide a suitable key function.

Note
Note

The original iterable is not changed by sorted, it always returns a new list. If you want to sort a list in place, use the list.sort() method instead.

Opgave

Swipe to start coding

Sort a list of tuples by the second element in each tuple using the sorted function and a named function as the key parameter.

  • You are given a list named pairs containing tuples of two integers.
  • Define a function named get_second_element that takes a tuple and returns its second value.
  • Use the sorted function and pass get_second_element as the key parameter to sort pairs by the second value in each tuple.
  • Store the result in a variable named sorted_pairs.
  • Do not forget to remove pass.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 4
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt