Python | Add list elements with a multi-list based on index
Last Updated :
17 Apr, 2023
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
Python3
List = [ 1 , 2 , 3 , 4 , 5 , 6 ]
List_of_List = [[ 0 ], [ 0 , 1 , 2 ], [ 0 , 1 ],
[ 0 , 1 ], [ 0 , 1 , 2 ], [ 0 ]]
Output = []
for x in range ( len ( List )):
temp = []
for y in List_of_List[x]:
temp.append(y + List [x])
Output.append(temp)
print ("Initial list is :", List )
print ("Initial list of list is :", List_of_List)
print ("Output is ", Output)
|
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]]
Time Complexity: O(n*n) where n is the number of elements in the list
Auxiliary Space: O(n), where n is the number of elements in the new output list
Method #2: Using enumerate()
Python3
List = [ 1 , 2 , 3 , 4 , 5 , 6 ]
List_of_List = [[ 0 ], [ 0 , 1 , 2 ], [ 0 , 1 ], [ 0 , 1 ], [ 0 , 1 , 2 ], [ 0 ]]
Output = []
Output = [[elem + List [x] for elem in y]
for x, y in enumerate (List_of_List)]
print ("Initial list is :", List )
print ("Initial list of list is :", List_of_List)
print ("Output is ", Output)
|
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]]
Time Complexity: O(n*n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #3: Using Zip()
Python3
List = [ 1 , 2 , 3 , 4 , 5 , 6 ]
List_of_List = [[ 0 ], [ 0 , 1 , 2 ], [ 0 , 1 ], [ 0 , 1 ], [ 0 , 1 , 2 ], [ 0 ]]
Output = []
Output = [[z + x for z in y ] for x, y in
zip ( List , List_of_List)]
print ("Initial list is :", List )
print ("Initial list of list is :", List_of_List)
print ("Output is ", Output)
|
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]]
Time Complexity: O(n*n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list
Method #4: Using map()
In this code, we are using the map() function to add the corresponding elements of the two lists, List and List_of_List. The map() function applies the lambda function on each element of the two lists, which adds the element of List to each element of the sublists in List_of_List.
The lambda function has two parameters, x and y, which represent the current element of List and the current sublist of List_of_List, respectively. The lambda function returns a new list which is the sublist of List_of_List with each element incremented by the corresponding element of List.
Finally, the map() function is wrapped in a call to list() to convert the map object returned by map() into a list.
Python3
List = [ 1 , 2 , 3 , 4 , 5 , 6 ]
List_of_List = [[ 0 ], [ 0 , 1 , 2 ], [ 0 , 1 ], [ 0 , 1 ], [ 0 , 1 , 2 ], [ 0 ]]
Output = []
Output = list ( map ( lambda x, y: [z + x for z in y], List , List_of_List))
print ( "Initial list is:" , List )
print ( "Initial list of list is :" , List_of_List)
print ( "Output is" , Output)
|
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]]
The time complexity of this approach is O(n), where n is the length of the lists. The auxiliary space is also O(n), as we are creating a new list of the same size as the original lists.
Method #5: Using nested list comprehension
Python3
List = [ 1 , 2 , 3 , 4 , 5 , 6 ]
List_of_List = [[ 0 ], [ 0 , 1 , 2 ], [ 0 , 1 ],
[ 0 , 1 ], [ 0 , 1 , 2 ], [ 0 ]]
Output = [[y + List [x] for y in sublist] for x, sublist in enumerate (List_of_List)]
print ( "Initial list is:" , List )
print ( "Initial list of list is :" , List_of_List)
print ( "Output is" , Output)
|
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]]
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Add Elements of Two Lists in Python
Adding corresponding elements of two lists can be useful in various situations such as processing sensor data, combining multiple sets of results, or performing element-wise operations in scientific computing. List Comprehension allows us to perform the addition in one line of code. It provides us a
3 min read
Python | Multiply each element in a sublist by its index
Given a list of lists, the task is to multiply each element in a sublist by its index and return a summed list. Given below are a few methods to solve the problem. Method #1: Using Naive Method C/C++ Code # Python3 code to demonstrate # to multiply numbers with position # and add them to return num
4 min read
Group Elements at Same Indices in a Multi-List - Python
We are given a 2D list, we have to group elements at the same indices in a multi-list which means combining elements that are positioned at identical indices across different list. For example:If we have a 2D list: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] then grouping elements at the same indices would re
4 min read
Move One List Element to Another List - Python
The task of moving one list element to another in Python involves locating a specific element in the source list, removing it, and inserting it into the target list at a desired position. For example, if a = [4, 5, 6, 7, 3, 8] and b = [7, 6, 3, 8, 10, 12], moving 10 from b to index 4 in a results in
3 min read
Python - Adding K to each element in a list of integers
In Python, we often need to perform mathematical operations on each element. One such operation is adding a constant value, K, to every element in the list. In this article, we will explore several methods to add K to each element in a list. Using List ComprehensionList comprehension provides a conc
2 min read
Python - Add list elements to tuples list
Sometimes, while working with Python tuples, we can have a problem in which we need to add all the elements of a particular list to all tuples of a list. This kind of problem can come in domains such as web development and day-day programming. Let's discuss certain ways in which this task can be don
6 min read
Split a Python List into Sub-Lists Based on Index Ranges
The task of splitting a Python list into sub-lists based on index ranges involves iterating through a list of index pairs and extracting the corresponding slices. Given a list a and a list of tuples b, where each tuple represents a start and end index, the goal is to create sub-lists that include el
3 min read
Sorting List of Lists with First Element of Each Sub-List in Python
In Python, sorting a list of lists by the first element of each sub-list is a common task. Whether you're dealing with data points, coordinates, or any other structured information, arranging the lists based on the values of their first elements can be crucial. In this article, we will sort a list o
3 min read
Python - Operation to each element in list
Given a list, there are often when performing a specific operation on each element is necessary. While using loops is a straightforward approach, Python provides several concise and efficient methods to achieve this. In this article, we will explore different operations for each element in the list.
3 min read
How To Combine Multiple Lists Into One List Python
Combining multiple lists into a single list is a common operation in Python, and there are various approaches to achieve this task. In this article, we will see how to combine multiple lists into one list in Python. Combine Multiple Lists Into One List PythonBelow are some of the ways by which we ca
2 min read