list_sort function Sorts the argument input list (nums) in descending orderuser_code.py123def list_sort(nums): # write some code here passRestore original fileSave onlySave & Grade Unlimited attempts
Question
list_sort function Sorts the argument input list (nums) in descending orderuser_code.py123def list_sort(nums): # write some code here passRestore original fileSave onlySave & Grade Unlimited attempts
Solution
Here is a step-by-step solution to create a function list_sort that sorts a list in descending order:
- Define the function
list_sortthat takes one argument,nums, which is the list to be sorted.
def list_sort(nums):
- Use the
sort()method of the list to sort the list in ascending order. Thesort()method sorts the list in-place, meaning that it modifies the original list.
def list_sort(nums):
nums.sort()
- To sort the list in descending order, pass the argument
reverse=Trueto thesort()method. This will sort the list in the reverse of the natural order, which is descending order for numbers.
def list_sort(nums):
nums.sort(reverse=True)
- Since the
sort()method sorts the list in-place, you don't need to return anything from the function. The original list will be sorted when the function is done.
def list_sort(nums):
nums.sort(reverse=True)
You can use this function to sort a list of numbers in descending order like this:
numbers = [5, 1, 9, 3, 7]
list_sort(numbers)
print(numbers) # Output: [9, 7, 5, 3, 1]
This will print the list [9, 7, 5, 3, 1], which is the list numbers sorted in descending order.
Similar Questions
Write a class MyList that inherits from list:Public instance method: def print_sorted(self): that prints the list, but sorted (ascending sort)You can assume that all the elements of the list will be of type intYou are not allowed to import any moduleguillaume@ubuntu:~/0x0A$ cat 1-main.py#!/usr/bin/python3MyList = __import__('1-my_list').MyListmy_list = MyList()my_list.append(1)my_list.append(4)my_list.append(2)my_list.append(3)my_list.append(5)print(my_list)my_list.print_sorted()print(my_list)guillaume@ubuntu:~/0x0A$ ./1-main.py[1, 4, 2, 3, 5][1, 2, 3, 4, 5][1, 4, 2, 3, 5]guillaume@ubuntu:~/0x0A$
Define a function sort_list(list1). The function returns three lists: the original list, the list in ascending order, and the list in descending order. Remember: your function should return three lists.Examplelist1 = [10, 1, 9, 2, 8, 3, 7, 4, 6, 5]Expected output:[10, 1, 9, 2, 8, 3, 7, 4, 6, 5][1, 2, 3, 4, 5, 6, 7, 8, 9, 10][10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
TypeError Traceback (most recent call last)/tmp/ipykernel_888/4252856711.py in <module> 2 3 list1 = d.values----> 4 sorted (list1, key=lambda x:x[1], reverse = True)TypeError: 'builtin_function_or_method' object is not iterable
Define a function sort_grades(tup1). The argument tup1 is a tuple each composed of (student_name, score). The function returns a tuple with same elements of tup1 but sorted by their scores in descending order.Exampletup1 = (('Bob', 47), ('Claire', 61), ('Dominic', 79))Expected output:(('Dominic', 79), ('Claire', 61), ('Bob', 47))Hintfirst convert tup1 to a list. Since each element in this list is a tuple with two items (student_name, score), to sort the list according to the second item score, you can use Python built-in sorted function:sorted(iterable, key=None, reverse=False) function returns a sorted list from the iterable object(refer to similar examples in Chapter 4.1).convert the above list to a tuple again, and return the tuple
Which of the following methods can be used to sort an array in Python?Optionslength()sort()arrange()order()
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.