 Open in App
Not now

# Python – Summation in Dual element Records List

• Last Updated : 04 Feb, 2023

Sometimes, while working with Records list, we can have problem in which we perform the summation of dual tuple records and store it in list. This kind of application can occur over various domains. Lets discuss certain ways in which this task can be performed.

Method #1 : Using list comprehension This is one of the way to solve this problem. In this we perform summation of dual tuples in a list and iteration is performed inside comprehended list.

## Python3

 `# Python3 code to demonstrate``# Summation in Dual element Records List``# using list comprehension` `# Initializing list``test_list ``=` `[(``6``, ``7``), (``2``, ``4``), (``8``, ``9``), (``6``, ``2``)]` `# printing original lists``print``("The original ``list` `is` `: " ``+` `str``(test_list))` `# Summation in Dual element Records List``# using list comprehension``res ``=` `[ele[``0``] ``+` `ele[``1``] ``for` `ele ``in` `test_list]` `# printing result``print` `("Summation pairs ``in` `tuple` `list` `: " ``+` `str``(res))`

Output :

```The original list is : [(6, 7), (2, 4), (8, 9), (6, 2)]
Summation pairs in tuple list : [13, 6, 17, 8]```

Time complexity: O(n), where n is the number of elements in the input list “test_list”.
Auxiliary space: O(n), where n is the number of elements in the input list “test_list”. This is because a new list of size n is created to store the result of the summation.

Method #2 : Using reduce() + add() This is yet another way to perform this task. In this, we iterate through the list and perform summation using reduce() and add() respectively.

## Python3

 `# Python3 code to demonstrate``# Summation in Dual element Records List``# using reduce() + add()``from` `operator ``import` `add``from` `functools ``import` `reduce` `# Initializing list``test_list ``=` `[(``6``, ``7``), (``2``, ``4``), (``8``, ``9``), (``6``, ``2``)]` `# printing original lists``print``("The original ``list` `is` `: " ``+` `str``(test_list))` `# Summation in Dual element Records List``# using reduce() + add()``res ``=` `[``reduce``(add, sub, ``0``) ``for` `sub ``in` `test_list]` `# printing result``print` `("Summation pairs ``in` `tuple` `list` `: " ``+` `str``(res))`

Output :

```The original list is : [(6, 7), (2, 4), (8, 9), (6, 2)]
Summation pairs in tuple list : [13, 6, 17, 8]```

Time complexity: O(n), where n is the number of elements in the input list test_list. This is because the reduce() function takes O(n) time to process the elements in each tuple, and the for loop takes O(n) time to process all n tuples in test_list.
Auxiliary space: O(n), where n is the number of elements in the input list test_list. This is because the res list, which contains the sums of each tuple in test_list, takes up O(n) space.

Method #3 : Using sum(),list() methods

## Python3

 `# Python3 code to demonstrate``# Summation in Dual element Records List` `# Initializing list``test_list ``=` `[(``6``, ``7``), (``2``, ``4``), (``8``, ``9``), (``6``, ``2``)]` `# printing original lists``print``(``"The original list is : "` `+` `str``(test_list))` `# Summation in Dual element Records List``res``=``[]``for` `i ``in` `test_list:``    ``res.append(``sum``(``list``(i)))``    ` `# printing result``print` `(``"Summation pairs in tuple list : "` `+` `str``(res))`

Output

```The original list is : [(6, 7), (2, 4), (8, 9), (6, 2)]
Summation pairs in tuple list : [13, 6, 17, 8]```

My Personal Notes arrow_drop_up