Challenge - 5 Problems
Lambda Sorting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediate2:00remaining
Sorting a list of tuples by second element using lambda
What is the output of this code?
data = [(3, 2), (1, 5), (4, 1)] sorted_data = sorted(data, key=lambda x: x[1]) print(sorted_data)
Python
data = [(3, 2), (1, 5), (4, 1)] sorted_data = sorted(data, key=lambda x: x[1]) print(sorted_data)
Attempts:
2 left
๐ก Hint
Look at the second element of each tuple and how sorted() uses the lambda key.
โ Incorrect
The lambda function returns the second element of each tuple. sorted() arranges tuples by these values in ascending order.
โ Predict Output
intermediate2:00remaining
Sorting strings by length using lambda
What is the output of this code?
words = ['apple', 'banana', 'fig', 'date'] sorted_words = sorted(words, key=lambda w: len(w)) print(sorted_words)
Python
words = ['apple', 'banana', 'fig', 'date'] sorted_words = sorted(words, key=lambda w: len(w)) print(sorted_words)
Attempts:
2 left
๐ก Hint
Check the length of each word and how sorted() orders them.
โ Incorrect
The lambda returns the length of each word. sorted() orders words from shortest to longest.
โ Predict Output
advanced2:30remaining
Sorting a list of dictionaries by nested key using lambda
What is the output of this code?
items = [{'name': 'apple', 'info': {'price': 5}}, {'name': 'banana', 'info': {'price': 3}}, {'name': 'cherry', 'info': {'price': 7}}]
sorted_items = sorted(items, key=lambda x: x['info']['price'])
print([item['name'] for item in sorted_items])Python
items = [{'name': 'apple', 'info': {'price': 5}}, {'name': 'banana', 'info': {'price': 3}}, {'name': 'cherry', 'info': {'price': 7}}]
sorted_items = sorted(items, key=lambda x: x['info']['price'])
print([item['name'] for item in sorted_items])Attempts:
2 left
๐ก Hint
Look inside the nested dictionary to find the price for sorting.
โ Incorrect
The lambda accesses the nested 'price' key. sorted() orders items by price ascending.
โ Predict Output
advanced2:00remaining
Sorting with lambda and reverse order
What is the output of this code?
numbers = [10, 3, 7, 1] sorted_numbers = sorted(numbers, key=lambda x: -x) print(sorted_numbers)
Python
numbers = [10, 3, 7, 1] sorted_numbers = sorted(numbers, key=lambda x: -x) print(sorted_numbers)
Attempts:
2 left
๐ก Hint
The lambda negates each number, so sorting by negative values reverses order.
โ Incorrect
Sorting by negative values sorts numbers descending because bigger numbers have smaller negatives.
๐ง Conceptual
expert3:00remaining
Understanding lambda with sorted() behavior on complex keys
Given the code below, what is the output?
data = ['a1', 'a10', 'a2', 'a11'] sorted_data = sorted(data, key=lambda x: (x[0], int(x[1:]))) print(sorted_data)
Python
data = ['a1', 'a10', 'a2', 'a11'] sorted_data = sorted(data, key=lambda x: (x[0], int(x[1:]))) print(sorted_data)
Attempts:
2 left
๐ก Hint
The lambda sorts first by the letter, then by the number part as integer.
โ Incorrect
The key is a tuple: first character 'a' (same for all), then integer from substring. So numbers sort numerically, not lexicographically.