Python | Add list elements with a multi-list based on index
Given two lists, one is a simple list and second is a multi-list, the task is to add both lists based on index.
Example:
Input: List = [1, 2, 3, 4, 5, 6] List of list = [[0], [0, 1, 2], [0, 1], [0, 1], [0, 1, 2], [0]] Output: [[1], [2, 3, 4], [3, 4], [4, 5], [5, 6, 7], [6]] Explanation: [1] = [1+0] [2, 3, 4] = [0+2, 1+2, 2+2] [3, 4] = [3+0, 3+1] [4, 5] = [4+0, 4+1] [5, 6, 7] = [5+0, 5+1, 5+2] [6] = [6+0]
Let’s discuss some methods to do this task.
Method #1: Using iteration
# Python code to add list elements # with a multi-list based on index # List initialization List = [1, 2, 3, 4, 5, 6] List_of_List = [[0], [0, 1, 2], [0, 1], [0, 1], [0, 1, 2], [0]] Output = [] # Iteration for x in range(len(List)): temp = [] for y in List_of_List[x]: temp.append(y + List[x]) Output.append(temp) # Printing print("Initial list is:", List) print("Initial list of list is :", List_of_List) print("Output is", Output) |
chevron_right
filter_none
Output:
Initial list is: [1, 2, 3, 4, 5, 6] Initial list of list is : [[0], [0, 1, 2], [0, 1], [0, 1], [0, 1, 2], [0]] Output is [[1], [2, 3, 4], [3, 4], [4, 5], [5, 6, 7], [6]]
Method #2: Using enumerate()
# Python code to add list elements # with a multi-list based on index # List initialization List = [1, 2, 3, 4, 5, 6] List_of_List = [[0], [0, 1, 2], [0, 1], [0, 1], [0, 1, 2], [0]] Output = [] # using enumerate Output = [[elem + List[x] for elem in y] for x, y in enumerate(List_of_List)] # Printing print("Initial list is:", List) print("Initial list of list is :", List_of_List) print("Output is", Output) |
chevron_right
filter_none
Output:
Initial list is: [1, 2, 3, 4, 5, 6] Initial list of list is : [[0], [0, 1, 2], [0, 1], [0, 1], [0, 1, 2], [0]] Output is [[1], [2, 3, 4], [3, 4], [4, 5], [5, 6, 7], [6]]
Method #3: Using Zip()
# Python code to add list elements # with a multi-list based on index # List initialization List = [1, 2, 3, 4, 5, 6] List_of_List = [[0], [0, 1, 2], [0, 1], [0, 1], [0, 1, 2], [0]] Output = [] # using zip Output = [[z + x for z in y ]for x, y in zip(List, List_of_List)] # Printing print("Initial list is:", List) print("Initial list of list is :", List_of_List) print("Output is", Output) |
chevron_right
filter_none
Output:
Initial list is: [1, 2, 3, 4, 5, 6] Initial list of list is : [[0], [0, 1, 2], [0, 1], [0, 1], [0, 1, 2], [0]] Output is [[1], [2, 3, 4], [3, 4], [4, 5], [5, 6, 7], [6]]
Recommended Posts:
- Python | Group list elements based on frequency
- Python | Replace elements in second list with index of same element in first list
- Python | Equate two list index elements
- Python | Filter list of strings based on the substring list
- Python | Filter a list based on the given list of strings
- Python | Increment 1's in list based on pattern
- Python | Split list of strings into sublists based on length
- Python | Filter dictionary key based on the values in selective list
- Python | Sort list of list by specified index
- Python list | index()
- Python | Accessing index and value in list
- Python | Subgroups of i'th index size in list
- Python | Separate odd and even index elements
- Python | Index specific cyclic iteration in list
- Python | Find mismatch item on same index in two list
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.



