Sometimes, while working with Python lists, we can have a problem in which we need to perform addition of lists in diagonal manner that i.e. adding ith element of 1 list to i + 1 element of other list. This kind of problem can have application in day-day programming. Lets discuss certain ways in which this task can be performed.

**Method #1 : Using loop**

This is brute force way in which this task can be performed. In this, we iterate one list and test add the i + 1th element of other list and construct the resultant list.

`# Python3 code to demonstrate ` `# Diagonal element addition among lists ` `# using loop ` ` ` `# Initializing lists ` `test_list1 ` `=` `[` `1` `, ` `6` `, ` `8` `, ` `5` `, ` `3` `] ` `test_list2 ` `=` `[` `8` `, ` `10` `, ` `3` `, ` `4` `, ` `5` `] ` ` ` `# printing original lists ` `print` `(` `"The original list 1 is : "` `+` `str` `(test_list1)) ` `print` `(` `"The original list 2 is : "` `+` `str` `(test_list2)) ` ` ` `# Diagonal element addition among lists ` `# using loop ` `res ` `=` `[] ` `for` `idx ` `in` `range` `(` `0` `, ` `len` `(test_list1) ` `-` `1` `): ` ` ` `res.append(test_list1[idx] ` `+` `test_list2[idx ` `+` `1` `]) ` ` ` `# printing result ` `print` `(` `"List after diagonal addition : "` `+` `str` `(res)) ` |

*chevron_right*

*filter_none*

**Output :**

The original list 1 is : [1, 6, 8, 5, 3] The original list 2 is : [8, 10, 3, 4, 5] List after diagonal addition : [11, 9, 12, 10]

**Method #2 : Using zip() + list comprehension**

This is yet another way in which this task can be performed. In this, we group ith with i+1th element of other list using zip(). The task of performing addition is done in list comprehension.

`# Python3 code to demonstrate ` `# Diagonal element addition among lists ` `# using zip() + list comprehension ` ` ` `# Initializing lists ` `test_list1 ` `=` `[` `1` `, ` `6` `, ` `8` `, ` `5` `, ` `3` `] ` `test_list2 ` `=` `[` `8` `, ` `10` `, ` `3` `, ` `4` `, ` `5` `] ` ` ` `# printing original lists ` `print` `(` `"The original list 1 is : "` `+` `str` `(test_list1)) ` `print` `(` `"The original list 2 is : "` `+` `str` `(test_list2)) ` ` ` `# Diagonal element addition among lists ` `# using zip() + list comprehension ` `res ` `=` `[i ` `+` `j ` `for` `i, j ` `in` `zip` `(test_list1, test_list2[` `1` `:])] ` ` ` `# printing result ` `print` `(` `"List after diagonal addition : "` `+` `str` `(res)) ` |

*chevron_right*

*filter_none*

**Output :**

The original list 1 is : [1, 6, 8, 5, 3] The original list 2 is : [8, 10, 3, 4, 5] List after diagonal addition : [11, 9, 12, 10]

## Recommended Posts:

- Python - Count of matching elements among lists (Including duplicates)
- Python - Row-wise element Addition in Tuple Matrix
- Python | Zipping two lists of lists
- Python | Program to count number of lists in a list of lists
- Python - Convert Lists into Similar key value lists
- Addition and Blending of images using OpenCV in Python
- Python | Addition of tuples
- Addition in Nested Tuples - Python
- Python - Pairwise Addition in Tuples
- Python | Rear Addition of Record
- Python program for addition and subtraction of complex numbers
- Python - Specific Range Addition in List
- Python | Alternate character addition
- Addition and Subtraction on TimeDelta objects using Pandas - Python
- Arithmetic Operations on Images using OpenCV | Set-1 (Addition and Subtraction)
- Python | Check if two lists have at-least one element common
- Python | Concatenate two lists element-wise
- Python | Check if two lists have any element in common
- Python | Check if element exists in list of lists
- Python | Remove given element from list of lists

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.