- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
- Python 3 Advanced Tutorial
- Python 3 - Classes/Objects
- Python 3 - Reg Expressions
- Python 3 - CGI Programming
- Python 3 - Database Access
- Python 3 - Networking
- Python 3 - Sending Email
- Python 3 - Multithreading
- Python 3 - XML Processing
- Python 3 - GUI Programming
- Python 3 - Further Extensions
Python Program to print a specific number of rows with Maximum Sum
When it is required to print a specific number of rows with the maximum sum, the ‘sorted’ method, and the ‘lambda’ method are used.
Below is a demonstration of the same
my_list = [[2, 4, 6, 7], [2, 4, 8], , [1, 3, 5, 6], [8, 2, 1]] print("The list is :") print(my_list) my_key = 3 print("The key is") print(my_key) my_result = sorted(my_list, key=lambda row: sum(row), reverse=True)[:my_key] print("The resultant list is :") print(my_result)
The list is : [[2, 4, 6, 7], [2, 4, 8], , [1, 3, 5, 6], [8, 2, 1]] The key is 3 The resultant list is : [, [2, 4, 6, 7], [1, 3, 5, 6]]
A list of list is defined and is displayed on the console.
A key value is defined and is displayed on the console.
The ‘sorted’ method is used on the list along with the lambda method, where the sum of the elements is determined, and the elements are reversed based on the key value.
This is assigned to a variable.
This is displayed as output on the console.
- Related Articles
- Python Program to Filter Rows with a specific Pair Sum
- Maximum sum of pairs with specific difference C++ program
- Python - Sum only specific rows of a Pandas Dataframe
- Python Program to print element with maximum vowels from a List
- Program to find number of columns flips to get maximum number of equal Rows in Python?
- Program to find sum of contiguous sublist with maximum sum in Python
- Program to find maximum number of non-overlapping subarrays with sum equals target using Python
- Python Pandas - Display specific number of rows from a DataFrame
- Maximum sum of pairs with specific difference in C++
- Write a program in Python to print dataframe rows as orderDict with a list of tuple values
- How can Tensorflow be used to sum specific elements/rows of a matrix in Python?
- Program to print maximum number of characters by copy pasting in n steps in Python?
- Write a program in Python to read CSV data from a file and print the total sum of last two rows
- Python program to print number triangle
- Python program to sort matrix based upon sum of rows