0
0
Pythonprogramming~20 mins

Lambda with sorted() in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Lambda Sorting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2: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)
A[(1, 5), (3, 2), (4, 1)]
B[(4, 1), (3, 2), (1, 5)]
C[(3, 2), (4, 1), (1, 5)]
D[(4, 1), (1, 5), (3, 2)]
Attempts:
2 left
๐Ÿ’ก Hint
Look at the second element of each tuple and how sorted() uses the lambda key.
โ“ Predict Output
intermediate
2: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)
A['fig', 'date', 'apple', 'banana']
B['banana', 'apple', 'date', 'fig']
C['apple', 'banana', 'date', 'fig']
D['date', 'fig', 'apple', 'banana']
Attempts:
2 left
๐Ÿ’ก Hint
Check the length of each word and how sorted() orders them.
โ“ Predict Output
advanced
2: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])
A['apple', 'banana', 'cherry']
B['cherry', 'apple', 'banana']
C['banana', 'apple', 'cherry']
D['banana', 'cherry', 'apple']
Attempts:
2 left
๐Ÿ’ก Hint
Look inside the nested dictionary to find the price for sorting.
โ“ Predict Output
advanced
2: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)
A[1, 3, 7, 10]
B[10, 3, 7, 1]
C]01 ,7 ,3 ,1[
D[10, 7, 3, 1]
Attempts:
2 left
๐Ÿ’ก Hint
The lambda negates each number, so sorting by negative values reverses order.
๐Ÿง  Conceptual
expert
3: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)
A['a1', 'a2', 'a10', 'a11']
B['a1', 'a10', 'a11', 'a2']
C['a10', 'a11', 'a1', 'a2']
D['a11', 'a10', 'a2', 'a1']
Attempts:
2 left
๐Ÿ’ก Hint
The lambda sorts first by the letter, then by the number part as integer.