Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Sorted Fundamentals | Higher-Order Functions and Lambdas
Functional Programming Concepts in Python
Sección 2. Capítulo 4
single

single

Sorted Fundamentals

Desliza para mostrar el menú

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.

Tarea

Desliza para comenzar a programar

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.

Solución

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 4
single

single

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

some-alt