0
0
Pythonprogramming~20 mins

Sorting and reversing lists in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Sorting and Reversing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of sorting and reversing a list
What is the output of this Python code?
Python
numbers = [5, 3, 8, 1, 2]
numbers.sort()
numbers.reverse()
print(numbers)
A[2, 1, 3, 5, 8]
B[1, 2, 3, 5, 8]
C[5, 3, 8, 1, 2]
D[8, 5, 3, 2, 1]
Attempts:
2 left
๐Ÿ’ก Hint
Remember that sort() arranges the list in ascending order, then reverse() flips it.
โ“ Predict Output
intermediate
2:00remaining
Output of sorted() vs list.sort()
What is the output of this code snippet?
Python
fruits = ['banana', 'apple', 'cherry']
sorted_fruits = sorted(fruits)
print(fruits)
print(sorted_fruits)
A
['apple', 'banana', 'cherry']
['apple', 'banana', 'cherry']
B
['banana', 'apple', 'cherry']
['apple', 'banana', 'cherry']
C
['banana', 'apple', 'cherry']
['banana', 'apple', 'cherry']
D
['cherry', 'banana', 'apple']
['apple', 'banana', 'cherry']
Attempts:
2 left
๐Ÿ’ก Hint
sorted() returns a new sorted list, list.sort() changes the list itself.
๐Ÿง  Conceptual
advanced
2:00remaining
Effect of reverse parameter in sort()
What will be the output of this code?
Python
nums = [4, 1, 7, 3]
nums.sort(reverse=True)
print(nums)
A[7, 4, 3, 1]
B[1, 3, 4, 7]
C[4, 1, 7, 3]
D[3, 1, 4, 7]
Attempts:
2 left
๐Ÿ’ก Hint
The reverse=True parameter sorts the list in descending order.
โ“ Predict Output
advanced
2:00remaining
Output after multiple reverses
What is the output of this code?
Python
letters = ['a', 'b', 'c', 'd']
letters.reverse()
letters.reverse()
print(letters)
A['a', 'b', 'c', 'd']
B['d', 'c', 'b', 'a']
C['b', 'a', 'd', 'c']
D['c', 'd', 'a', 'b']
Attempts:
2 left
๐Ÿ’ก Hint
Reversing twice returns the list to original order.
โ“ Predict Output
expert
3:00remaining
Output of sorting with a custom key and reversing
What is the output of this code?
Python
words = ['apple', 'banana', 'pear', 'kiwi']
words.sort(key=len)
words.reverse()
print(words)
A['pear', 'kiwi', 'apple', 'banana']
B['kiwi', 'pear', 'apple', 'banana']
C['banana', 'apple', 'pear', 'kiwi']
D['banana', 'pear', 'apple', 'kiwi']
Attempts:
2 left
๐Ÿ’ก Hint
The list is sorted by word length ascending, then reversed to descending length.