Knowee
Questions
Features
Study Tools

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]

Question

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]

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a Python function that does exactly what you asked for:

def sort_list(list1):
    # Original list
    original_list = list1.copy()
    
    # Ascending order
    ascending_list = sorted(list1)
    
    # Descending order
    descending_list = sorted(list1, reverse=True)
    
    return original_list, ascending_list, descending_list

You can use this function like this:

list1 = [10, 1, 9, 2, 8, 3, 7, 4, 6, 5]
original, ascending, descending = sort_list(list1)
print(original)
print(ascending)
print(descending)

This will 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]

This problem has been solved

Similar Questions

SortingEvery Python list has a sort method that sorts it in place. If you don’t want to messup your list, you can use the sorted function, which returns a new list:x = [4, 1, 2, 3]y = sorted(x) # y is [1, 2, 3, 4], x is unchangedx.sort() # now x is [1, 2, 3, 4]By default, sort (and sorted) sort a list from smallest to largest based on naivelycomparing the elements to one another.If you want elements sorted from largest to smallest, you can specify a reverse=Trueparameter. And instead of comparing the elements themselves, you can compare theresults of a function that you specify with key:# sort the list by absolute value from largest to smallestx = sorted([-4, 1, -2, 3], key=abs, reverse=True) # is [-4, 3, -2, 1]# sort the words and counts from highest count to lowestwc = sorted(word_counts.items(),key=lambda word_and_count: word_and_count[1],reverse=True)

my_first_list = ['a', 'b', 'c']my_second_list = ['d', 'e', 'f', 'g']my_third_list = my_second_list + my_first_listmy_third_list.pop()my_third_list.sort()print (my_third_list)

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_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

Given the following list of numbers: [21, 1, 26, 45, 29, 28, 2, 9, 16, 49, 39, 27, 43, 34, 46, 40] which answer illustrates the list to be sorted after 3 recursive calls to mergesort? Select one: a. [21,1] b. [16, 49, 39, 27, 43, 34, 46, 40] c. [21] d. [21, 1, 26, 45]

1/3

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.