In this Python list program, we will take a user input as a list of lists and sort a list by the sum of sublists with the help of the below-given steps.
Sort list by sum of sublists:
Steps to solve the program
- Take a list of lists as input.
- Sort the list by the sum of sublists using sorted() and set the key=sum.
- Print the output to see the result.
#Input list
list1 = [[3, 5, 6], [2, 1, 3], [5, 1, 1],
[1, 2, 1], [0, 4, 1]]
#Printing output
print(sorted(list1, key=sum))
Output :
[[1, 2, 1], [0, 4, 1], [2, 1, 3], [5, 1, 1], [3, 5, 6]]
Related Articles
Python program to extract the specified sizes of strings from a given list of string values.
Python program to find the difference between consecutive numbers in a given list.
Python program to calculate the average of the given list.
Python program to count integers in a given mixed list.
Python program to access multiple elements of the specified index from a given list.
Python program to check whether a specified list is sorted or not.