VOOZH about

URL: https://www.geeksforgeeks.org/python/python-add-list-elements-with-a-multi-list-based-on-index/

⇱ Python | Add list elements with a multi-list based on index - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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 

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() 

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() 

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.


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


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)

Comment