# Python | Common Row elements Summation

The problem of finding the common elements in list of 2 lists is quite a common problem and can be dealt with ease and also has been discussed before many times. But sometimes, we require to find the elements that are in common from N lists and return their sum. Let’s discuss certain ways in which this operation can be performed.

Method #1 : Using `reduce() + lambda + set() + sum()`
This particular task can be achieved in just a one line using the combination of the above functions. The reduce function can be used to operate the function of “&” operation to all the list. The set function can be used to convert list into a set to remove repetition. The task of performing sum is done using sum().

 `# Python code to demonstrate  ` `# Common Row elements Summation ` `# using reduce() + lambda + set() + sum() ` ` `  `# initializing list of lists ` `test_list ``=` `[[``2``, ``3``, ``5``, ``8``], [``2``, ``6``, ``7``, ``3``], [``10``, ``9``, ``2``, ``3``]] ` ` `  `# printing original list ` `print` `(``"The original list is : "` `+` `str``(test_list)) ` ` `  `# Common Row elements Summation ` `# using reduce() + lambda + set() + sum() ` `res ``=` `sum``(``list``(``reduce``(``lambda` `i, j: i & j, (``set``(x) ``for` `x ``in` `test_list)))) ` ` `  `# printing result ` `print` `(``"The common row elements sum is : "` `+` `str``(res)) `

Output :

```The original list is : [[2, 3, 5, 8], [2, 6, 7, 3], [10, 9, 2, 3]]
The common row elements sum is : 5
```

Method #2 : Using `map() + intersection() + sum()`
The map function can be used to convert each of the lists to set to be operated by to perform the intersection, using the set.intersection function. This is the most elegant way to perform this particular task. The task of performing sum is done using sum().

 `# Python3 code to demonstrate  ` `# Common Row elements Summation ` `# using map() + intersection() + sum() ` ` `  `# initializing list of lists ` `test_list ``=` `[[``2``, ``3``, ``5``, ``8``], [``2``, ``6``, ``7``, ``3``], [``10``, ``9``, ``2``, ``3``]] ` ` `  `# printing original list ` `print` `(``"The original list is : "` `+` `str``(test_list)) ` ` `  `# Common Row elements Summation ` `# using map() + intersection() + sum() ` `res ``=` `sum``(``list``(``set``.intersection(``*``map``(``set``, test_list)))) ` ` `  `# printing result ` `print` `(``"The common row elements sum is : "` `+` `str``(res)) `

Output :

```The original list is : [[2, 3, 5, 8], [2, 6, 7, 3], [10, 9, 2, 3]]
The common row elements sum is : 5
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.