• Register
0 votes
260 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.

7.5k points

Please log in or register to answer this question.

2 Answers

0 votes

Solution:

I have faced the same Problem in the recent past. I have the  following solution on your problem.

Depending on the contents of list, there different approaches and they can be used according to need. In the easiest possible case, when the play_list is considered the list of integers or strings, the sorted() function must be used as follows:

 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 the contents of play_list are complex objects e.g. lists or class object  the key parameter must  be specified that will be used for comparisons as follows:

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']]

So I will suggest the following approaches:

1) If the elements are atomic, then you can use sorted(play_list);

2) If the elements are complex, then you can use sorted(play_list, key=key)

38.6k points
0 votes

Solution:

The sort() method sorts the elements of a given list.

The sort() method sorts the elements of a given list in a specific ascending or descending order.

The syntax of the sort() method is:

list.sort(key=..., reverse=...)

Alternatively, you can also use Python's built-in sorted() function for the same purpose.

sorted(list, key=..., reverse=...)

Example: sort a given list

# vowels list
vowels = ['e', 'a', 'u', 'o', 'i']

# sort the vowels
vowels.sort()

# print vowels
print('Sorted list:', vowels)

Sorting user using user defined order.

list_name.sort(key=…, reverse=…) – it sorts according to user’s choice
# Python program to demonstrate sorting by user's 
# choice 

# function to return the second element of the 
# two elements passed as the parameter 
def sortSecond(val): 
	return val[1] 

# list1 to demonstrate the use of sorting 
# using using second key 
list1 = [(1, 2), (3, 3), (1, 1)] 

# sorts the array in ascending according to 
# second element 
list1.sort(key = sortSecond) 
print(list1) 

# sorts the array in descending according to 
# second element 
list1.sort(key = sortSecond, reverse = True) 
print(list1) 

 

31.7k points

Related questions

0 votes
1 answer 44 views
44 views
Problem: Given that play_list has been defined to be a list, write a statement that sorts the list?
asked Mar 9, 2020 ArifulIslam 9.9k points
0 votes
1 answer 19 views
19 views
Problem: I have no idea to solve this, somebody help : Given a list named play_list, write an expression whose value is the length of play_list.
asked Jun 12 asha 121k points
0 votes
1 answer 13 views
13 views
Problem: I am asked to reverse a which takes head as a parameter whereas head is a linked list The function I have written for reversing the list is giving [3,2,1,2,1,2,1,2,1] and works only for a list of length 3. How can I generalize it for a list of length n?
asked Apr 25 ummeshani 10.8k points
0 votes
1 answer 203 views
203 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 7 views
7 views
Problem: Asked few friends however no response, please help : This is a value that signals when the end of a list of values has been reached.
asked Jun 14 asha 121k points
0 votes
1 answer 13 views
13 views
Problem: Is there anyone who knows the answer: Write a program that reads in a list of integers into an array with base type int?
asked Jun 13 Nahil 128k points
0 votes
1 answer 12 views
12 views
Problem: Can someone please help fix this > Write a program that reads in three strings and sorts them lexicographically
asked May 1 ummeshani 10.8k points
0 votes
3 answers 1.4k views
1.4k views
Problem : Python Program to Calculate the Average of Numbers in a Given List
asked Oct 21, 2019 peterlaw 6.9k points
0 votes
1 answer 33 views
33 views
Problem: I was trying to solve a problem in Codility provided below, Write a function: class Solution { public int solution(int[] A); } that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A. For example, given A = [1, 3, 6, 4, 1, 2], the function ... for (int i = 0; i < N; i++) { if (C[i] != 0) { return (i + 1); } } return (N + 1); }
asked Apr 25 sumaiya simi 43.9k points
0 votes
1 answer 125 views
125 views
Problem: How can I write a program that reads an unspecified number of integers and then prints how many numbers the user has entered and their sum without using a list or array?
asked Feb 27 Code Learner 10.5k points