What is the output of the following? “`python data = [3, 1, 4, 1, 5, 9, 2, 6] result = sorted(data, key=lambda x: -x) print(result[:3]) “`

Python Developer Hard

Python Developer — Hard

What is the output of the following? “`python data = [3, 1, 4, 1, 5, 9, 2, 6] result = sorted(data, key=lambda x: -x) print(result[:3]) “`

Key points

  • The `sorted` function can take a custom key for sorting.
  • The lambda function `-x` sorts the list in descending order.
  • Slicing `result[:3]` extracts the first three elements.
  • The correct answer is the three largest numbers in descending order.

Ready to go further?

Related questions