Write a Python program to sort a dictionary – Based on the ValuesConstraints:Input Format:DictionaryOutput Format:After Sorting - Key and Value needs to be printed one by one with a space separating each<Key> <Value>Example:Input:{2: 56, 1: 12, 5: 122, 4: 24, 6: 18, 3: 323}Output:1 126 184 242 565 1223 323 Explanation:NAPublic Test Cases:# INPUT EXPECTED OUTPUT1 {2: 56, 1: 12, 5: 122, 4: 24, 6: 18, 3: 323}1 126 184 242 565 1223 323
Question
Write a Python program to sort a dictionary – Based on the ValuesConstraints:Input Format:DictionaryOutput Format:After Sorting - Key and Value needs to be printed one by one with a space separating each<Key> <Value>Example:Input:{2: 56, 1: 12, 5: 122, 4: 24, 6: 18, 3: 323}Output:1 126 184 242 565 1223 323 Explanation:NAPublic Test Cases:# INPUT EXPECTED OUTPUT1 {2: 56, 1: 12, 5: 122, 4: 24, 6: 18, 3: 323}1 126 184 242 565 1223 323
Solution
Here is a Python program that sorts a dictionary based on its values:
# Define the dictionary
dict = {2: 56, 1: 12, 5: 122, 4: 24, 6: 18, 3: 323}
# Sort the dictionary based on its values
sorted_dict = {k: v for k, v in sorted(dict.items(), key=lambda item: item[1])}
# Print the sorted dictionary
for key, value in sorted_dict.items():
print(key, value)
This program works by using the sorted function to sort the items in the dictionary based on their values. The key argument of the sorted function is a function that takes an item from the dictionary and returns a value that sorted should use to sort the items. In this case, we're using a lambda function that returns the value of each item, which causes sorted to sort the items based on their values.
The sorted items are then used to create a new dictionary, which is stored in sorted_dict. Finally, the program prints the keys and values of the sorted dictionary one by one.
Similar Questions
The following data is connected to a Sort tool. The data type for the column is V_String.The Sort tool is configured to sort on the Value column in Ascending order, with Use Dictionary Order unchecked. What is the output of the Sort tool? A.B.C.
What order do the keys print in after the following code is run?counts = {'annie' : 42, 'chuck' : 1, 'jan' : 100}items = counts.items()out = sorted(items, key = lambda t: t[1])for item in out: print(item[0]) jan, chuck, anniechuck, annie, janannie, chuck, janjan, annie, chuck
You work in the examination department of a school, and you've been given a task to sort the test scores of students in ascending order. The scores are stored in a list where each element represents the score of a student.Input FormatThe first line contains the integer N, the size of the array. The next line contains N space-separated integers.Constraints• 1<=N<=1000 • -1000<=a[i]<=1000Output FormatPrint the array as a row of space-separated integers in each iteration.Sample Input 01010 9 8 7 6 5 4 3 2 1Sample Output 05 9 8 7 6 10 4 3 2 15 4 8 7 6 10 9 3 2 15 4 3 7 6 10 9 8 2 15 4 3 2 6 10 9 8 7 15 4 3 2 1 10 9 8 7 63 4 5 2 1 10 9 8 7 63 2 5 4 1 10 9 8 7 61 2 3 4 5 10 9 8 7 61 2 3 4 5 10 9 8 7 61 2 3 4 5 10 9 8 7 61 2 3 4 5 8 9 10 7 61 2 3 4 5 8 7 10 9 61 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 101 2 3 4 5 6 7 8 9 10
Suppose a courses score of a class is saved in a dictionarydata = {'Tom': [84, 96, 90], 'Bob':[ 67, 82, 76], 'Jack': [62, 90, 67], 'Linda': [78, 66,90]}Write a function to calculate the average score for each student, round the float value to integer, and save the average score in a dictionary, and then sort the dictionary based on the average score in reverse order and save in a list. return the dictionary and list.output:({'Tom': 90, 'Bob': 75, 'Jack': 73, 'Linda': 78},[('Tom', 90), ('Linda', 78), ('Bob', 75), ('Jack', 73)])
Write a Python program to create a new dictionary by extracting the mentioned keys and create a new new dictionary and print it as the result.Constraints:Input Format:Output Format: Example:Input:{"name": "Kelly","age": 25,"salary": 8000,"city": "New york"}["name", "salary"]Output:{'name': 'Kelly', 'salary': 8000}Explanation:Dictionary = {"name": "Kelly","age": 25,"salary": 8000,"city": "New york"}Keys that needs to be extracted = ["name", "salary"]Result = {'name': 'Kelly', 'salary': 8000}Public Test Cases:# INPUT EXPECTED OUTPUT1 {"name": "Kelly","age": 25,"salary": 8000,"city": "New york"} ["name", "salary"] {'name': 'Kelly', 'salary': 8000}
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.