Python Methods and Functions

Examples:

Input: [[1, 2, 3], [4, 5, 6], [10, 11, 12], [7, 8, 9]] Output: 33 Explanation: sum of all lists in the given list of lists are: list1 = 6, list2 = 15, list3 = 33, list4 = 24 so the maximum among these is of Input: [[3, 4, 5], [1, 2, 3], [0, 9, 0]] Output: 12

** Method 1: list traversal in lists **

We can move through the lists within the list and sum all the elements in the given list and use the ** max function ** to get the maximum sum of all the elements in the lists of the list.

` `
` ` |

** Exit: **

33

** Method 2: traversing the list **

Traverse only the outer list and sum all the elements in the inner lists using the

` def `

` maximumSum (list1): `

` maxi `

` = `

` 0 `

` # workaround `

` for `

` x `

` in `

` list1: `

` maxi `

` = `

` max `

` (`

` sum `

` (x) , maxi) `

` return `

` maxi `

` # driver code `

` list1 `

` = `

` [[`

` 1 `

` , `

` 2 `

`, `

` 3 `

`], [`

` 4 `

`, `

` 5 `

`, `

` 6 `

`], [`

` 10 `

`, `

` 11 `

`, `

` 12 `

`], [`

` 7 `

`, `

` 8 `

`, `

` 9 `

`]] `

` print `

` maximumSum (list1) `

** Exit:**

33

** Method 3: Sum and Max Function **

sum (max (list1, key = sum))

The above function syntax max () allows us to find the sum of a list in a list using ** key = sum **. ** max (list1, key = sum) **, this finds the list with the maximum sum of elements, and then ** sum (max (list1, key = sum)) ** returns us the sum of that list.

` `

` ` ` # Python program to search `

` # list in the list of lists whose `

` # the sum of the elements is the highest `

` # using the sum and max function `

` def `

` maximumSum (list1): `

` return `

` (`

` sum `

` (`

` max `

` (list1, key `` = `

` sum `

`))) `

` # driver code `

` list1 `

` = `

` [[`

` 1 `

`, `

` 2 `

`, `

` 3 `

`], [`

` 4 `

`, `

` 5 `

`, `` 6 `

`], [`

` 10 `

` , `

` 11 `

`, `

` 12 `

`], [`

` 7 `

`, `

` 8 `

`, `

` 9 `

`]] `

` ` ` prin t `

` maximumSum (list1) `

` `

** Exit :**

33

This book is an indispensable guide focuses on Machine Learning and R Programming, in an instructive and conversational tone which helps them who want to make their career in Big Data Analytics/ Data ...

10/07/2020

In the last decade, we have seen the impact of exponential advances in technology on the way we work, shop, communicate, and think. At the heart of this change is our ability to collect and gain insig...

10/07/2020

The rate at which we produce data is growing steadily, thus creating even larger streams of continuously evolving data. Online news, micro-blogs, search queries are just a few examples of these contin...

10/07/2020

Big data is, admittedly, an overhyped buzzword used by software and hardware companies to boost their sales. Behind the hype, however, there is a real and extremely important technology trend with imp...

10/07/2020

X
# Submit new EBook