# Python | Triple List Summation

There can be an application requirement to append elements of 2-3 lists to one list and perform summation. This kind of application has a potential to come into the domain of Machine Learning or sometimes in web development as well. Let’s discuss certain ways in which this particular task can be performed.

Method #1 : Using + operator + sum() This can be easily done using the plus operator as it does the element addition at the back of list. Similar logic is extended in case of multiple lists. The summation is performed using sum().

## Python3

 `# Python3 code to demonstrate ` `# Triple List Summation` `# using + operator + sum()`   `# initializing lists` `test_list1 ``=` `[``1``, ``3``, ``5``, ``5``, ``4``]` `test_list2 ``=` `[``4``, ``6``, ``2``, ``8``, ``10``]` `test_list3 ``=` `[``7``, ``5``, ``2``, ``9``, ``11``]`   `# printing original lists` `print` `("The original ``list` `1` `is` `: " ``+` `str``(test_list1))` `print` `("The original ``list` `2` `is` `: " ``+` `str``(test_list2))` `print` `("The original ``list` `3` `is` `: " ``+` `str``(test_list3))`   `# using + operator + sum()` `# Triple List Summation` `test_list1 ``=` `sum``(test_list1 ``+` `test_list2 ``+` `test_list3)` `    `  `# printing result ` `print` `("The summed ``and` `modified ``list` `is` `: " ``+` `str``(test_list1))`

Output :

```The original list 1 is : [1, 3, 5, 5, 4]
The original list 2 is : [4, 6, 2, 8, 10]
The original list 3 is : [7, 5, 2, 9, 11]
The summed and modified list is : 82```

Time Complexity: O(n) where n is the number of elements in the list “test_list”. The + operator + sum() is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”.

Method #2 : Using itertools.chain() + sum() The chain function can also be employed to perform this particular tasks as it uses the iterator to perform this and hence offers a better performance over the above method. The summation is performed using sum().

## Python3

 `# Python3 code to demonstrate ` `# Triple List Summation` `# using itertools.chain() + sum()` `from` `itertools ``import` `chain`   `# initializing lists` `test_list1 ``=` `[``1``, ``3``, ``5``, ``5``, ``4``]` `test_list2 ``=` `[``4``, ``6``, ``2``, ``8``, ``10``]` `test_list3 ``=` `[``7``, ``5``, ``2``, ``9``, ``11``]`   `# printing original lists` `print` `("The original ``list` `1` `is` `: " ``+` `str``(test_list1))` `print` `("The original ``list` `2` `is` `: " ``+` `str``(test_list2))` `print` `("The original ``list` `3` `is` `: " ``+` `str``(test_list3))`   `# using itertools.chain() + sum()` `# Triple List Summation` `test_list1 ``=` `sum``(``list``(chain(test_list1, test_list2, test_list3)))` `    `  `# printing result ` `print` `("The summed ``and` `modified ``list` `is` `: " ``+` `str``(test_list1))`

Output :

```The original list 1 is : [1, 3, 5, 5, 4]
The original list 2 is : [4, 6, 2, 8, 10]
The original list 3 is : [7, 5, 2, 9, 11]
The summed and modified list is : 82```

Time Complexity: O(n*n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1), constant extra space is required

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next