Complete the code to sort the list in ascending order.
numbers = [5, 2, 9, 1] sorted_numbers = sorted([1]) print(sorted_numbers)
The sorted() function takes an iterable like a list and returns a new sorted list. Here, we pass the list numbers to it.
Complete the code to sort the list in descending order.
fruits = ['banana', 'apple', 'cherry'] sorted_fruits = sorted(fruits, [1]=True) print(sorted_fruits)
The sorted() function has a reverse parameter. Setting reverse=True sorts the list in descending order.
Fix the error in the code to sort words by their length.
words = ['cat', 'elephant', 'dog'] sorted_words = sorted(words, key=[1]) print(sorted_words)
The key parameter expects a function. The built-in function len returns the length of each word, which is used for sorting.
Fill both blanks to create a dictionary of words and their lengths, including only words longer than 3 characters.
word_lengths = {word: [1] for word in words if [2] > 3}The dictionary comprehension maps each word to its length using len(word). The condition filters words with length greater than 3.
Fill all three blanks to create a dictionary with uppercase words as keys, their original words as values, including only words longer than 4 characters.
result = [1]: [2] for word in words if [3] > 4
word.lower() instead of uppercase for keys.The dictionary comprehension uses the uppercase word as the key (word.upper()), the original word as the value, and filters words longer than 4 characters using len(word) > 4.