• Register
0 votes


Given that play_list has been defined to be a list, write a statement that sorts the list?

5 3 2
5,640 points

Please log in or register to answer this question.

1 Answer

0 votes


Depending on the contents of list, different approaches should be used. In easiest possible case, when play_list is list of integers or strings, sorted() function should be used:
play_list = [3, 1, 2, 5, 4]

sorted_play_list = sorted(play_list)

print(sorted_play_list) # [1, 2, 3, 4, 5]


play_list = ['cdgf', 'fs', 'aa', 'Zsdf', 'qvtr', 'fa']

sorted_play_list = sorted(play_list)

print(sorted_play_list) # ['Zsdf', 'aa', 'cdgf', 'fa', 'fs', 'qvtr']

If contents of play_list are complex objects (like lists or class object), the key parameter should specified, which will be used for comparisons:

play_list = [[2, 'b'],[1, 'c'],[3, 'a']]

# sort by second element of nettled list

sorted_play_list = sorted(play_list, key=lambda a: a[1])

print(sorted_play_list) # [[3, 'a'], [2, 'b'], [1, 'c']]



Sorting is ordering a list of objects. We can distinguish two types of sorting. If the number of objects is small enough to fits into the main memory, sorting is called internal sorting. To sort the whole table, select the table. Move the pointer over the letter above the column by which you want to sort. Click the arrow that appears, then choose a sorting option: Sort Ascending: Sort the data in alphabetical order (A to Z) or by increasing numerical values.

5 3 1
10,860 points

Related questions

0 votes
2 answers 140 views
Problem : I need solution for below problem using Python Given that play_list has been defined to be a list, write a statement that sorts the list.
asked Nov 24, 2019 alecxe 7.5k points
1 vote
1 answer 112 views
Problem: I am new in python programming language and I am very confused about programming logics. Supose, someone has given two already defined variables, i and j, write a statement that swaps their associated values. Can anyone help me with this problem?
asked Apr 9, 2020 LizzyM 6.1k points
0 votes
2 answers 19 views
Problem: Given the array a, write an expression that refers to the first element of the array?
asked Apr 12, 2020 ArifulIslam 5.6k points
0 votes
2 answers 135 views
Problem: I am enable to write statement which can define plist to be the empty list. Can anyone help? Write a statement that defines plist to be the empty list?
asked Feb 17, 2020 maddi86 5.4k points
0 votes
2 answers 39 views
Problem: In C++, you can create aliases to a previously defined data type by using the ____ statement?
asked Apr 12, 2020 ArifulIslam 5.6k points
0 votes
2 answers 558 views
Problem: which statement can be used to handle some of the runtime errors in a program?
asked Mar 3, 2020 JHill 200 points
0 votes
1 answer 117 views
Problem : Facing following issue related to list in Python given that a refers to a list, write the necessary code to reverse the elements of the list.
asked Nov 16, 2019 peterlaw 6.9k points
0 votes
1 answer 21 views
Problem: Write a conditional that assigns True to fever if temperature is greater than 98.6.?
asked Apr 18, 2020 ArifulIslam 5.6k points
0 votes
1 answer 26 views
Problem: Which of the following is NOT a rule that must be followed when naming identifiers? A. Identifiers for constants should use an underscore between words. B. Identifiers should use periods to separate words. C. Identifiers should list the data type at the end of the name. D. Identifiers can contain spaces.
asked Mar 9, 2020 ArifulIslam 5.6k points
1 vote
1 answer 56 views
Problem: Is a programming language that can be used to create a wide range of windows applications?
asked Mar 23, 2020 ArifulIslam 5.6k points