Python – Replace index elements with elements in Other List
Last Updated :
05 May, 2023
Sometimes, while working with Python data, we can have a problem in which we have two lists and we need to replace positions in one list with the actual elements from other list. Lets discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension This is one way to solve this problem. In this we just iterate through the list and assign the index value from one list to other.
Python3
test_list1 = [ 'Gfg' , 'is' , 'best' ]
test_list2 = [ 0 , 1 , 2 , 1 , 0 , 0 , 0 , 2 , 1 , 1 , 2 , 0 ]
print ("The original list 1 is : " + str (test_list1))
print ("The original list 2 is : " + str (test_list2))
res = [test_list1[idx] for idx in test_list2]
print ("The lists after index elements replacements is : " + str (res))
|
Output :
The original list 1 is : [‘Gfg’, ‘is’, ‘best’] The original list 2 is : [0, 1, 2, 1, 0, 0, 0, 2, 1, 1, 2, 0] The lists after index elements replacements is : [‘Gfg’, ‘is’, ‘best’, ‘is’, ‘Gfg’, ‘Gfg’, ‘Gfg’, ‘best’, ‘is’, ‘is’, ‘best’, ‘Gfg’]
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
Method #2 : Using map() + lambda The combination of above functions can be used to perform this task. In this, we perform task of extension of logic to every element using map() and lambda functions.
Python3
test_list1 = [ 'Gfg' , 'is' , 'best' ]
test_list2 = [ 0 , 1 , 2 , 1 , 0 , 0 , 0 , 2 , 1 , 1 , 2 , 0 ]
print ("The original list 1 is : " + str (test_list1))
print ("The original list 2 is : " + str (test_list2))
res = list ( map ( lambda idx: test_list1[idx], test_list2))
print ("The lists after index elements replacements is : " + str (res))
|
Output :
The original list 1 is : [‘Gfg’, ‘is’, ‘best’] The original list 2 is : [0, 1, 2, 1, 0, 0, 0, 2, 1, 1, 2, 0] The lists after index elements replacements is : [‘Gfg’, ‘is’, ‘best’, ‘is’, ‘Gfg’, ‘Gfg’, ‘Gfg’, ‘best’, ‘is’, ‘is’, ‘best’, ‘Gfg’]
Time complexity: O(M^N) as the number of combinations generated is M choose N.
Auxiliary space: O(M^N) as the size of the resultant list is also M choose N.
# Method #3 : Using numpy.take()
We can also perform this task by using numpy module. In this, we just have to pass the both list to numpy.take() and then assign the result to list.
Python3
import numpy as np
test_list1 = [ 'Gfg' , 'is' , 'best' ]
test_list2 = [ 0 , 1 , 2 , 1 , 0 , 0 , 0 , 2 , 1 , 1 , 2 , 0 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = np.take(test_list1, test_list2)
print ( "The lists after index elements replacements is : " + str (res))
|
Output :
The original list 1 is : [‘Gfg’, ‘is’, ‘best’]
The original list 2 is : [0, 1, 2, 1, 0, 0, 0, 2, 1, 1, 2, 0]
The lists after index elements replacements is : [‘Gfg’, ‘is’, ‘best’, ‘is’, ‘Gfg’, ‘Gfg’, ‘Gfg’, ‘best’, ‘is’, ‘is’, ‘best’, ‘Gfg’]
Time complexity: O(n)
Auxiliary Space: O(n)
Method #4: using a for loop and a dictionary
Python3
test_list1 = [ 'Gfg' , 'is' , 'best' ]
test_list2 = [ 0 , 1 , 2 , 1 , 0 , 0 , 0 , 2 , 1 , 1 , 2 , 0 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
index_map = {index: element for index, element in enumerate (test_list1)}
res = [index_map[idx] for idx in test_list2]
print ( "The lists after index elements replacements is : " + str (res))
|
Output
The original list 1 is : ['Gfg', 'is', 'best']
The original list 2 is : [0, 1, 2, 1, 0, 0, 0, 2, 1, 1, 2, 0]
The lists after index elements replacements is : ['Gfg', 'is', 'best', 'is', 'Gfg', 'Gfg', 'Gfg', 'best', 'is', 'is', 'best', 'Gfg']
Time complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using pandas library
step-by-step algorithm for implementing the approach
- Import the pandas module.
- Define the two original lists.
- Create a DataFrame object with one column containing the values from the first list.
- Use the loc[] function of the DataFrame to extract the values from the first list corresponding to the indices in the second list.
- Convert the resulting series object to a list.
- Assign the resulting list to a new variable.
- Print the resulting list.
Python3
import pandas as pd
test_list1 = [ 'Gfg' , 'is' , 'best' ]
test_list2 = [ 0 , 1 , 2 , 1 , 0 , 0 , 0 , 2 , 1 , 1 , 2 , 0 ]
df = pd.DataFrame({ 'a' : test_list1})
res = df.loc[test_list2, 'a' ].tolist()
print ( "The lists after index elements replacements is : " + str (res))
|
output
The lists after index elements replacements is : ['Gfg', 'is', 'best', 'is', 'Gfg', 'Gfg', 'Gfg', 'best', 'is', 'is', 'best', 'Gfg']
Time complexity: O(n)
Space complexity: O(n)
Method #5 : Using zip() function
Use the zip() function to combine test_list1 and test_list2 element-wise.
Loop through the resulting tuples obtained in step 1.
Append the corresponding element from test_list1 to a new list res.
Print the final result list res.
Python3
test_list1 = [ 'Gfg' , 'is' , 'best' ]
test_list2 = [ 0 , 1 , 2 , 1 , 0 , 0 , 0 , 2 , 1 , 1 , 2 , 0 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = [test_list1[i] for i in test_list2]
print ( "The lists after index elements replacements is : " + str (res))
|
Output
The original list 1 is : ['Gfg', 'is', 'best']
The original list 2 is : [0, 1, 2, 1, 0, 0, 0, 2, 1, 1, 2, 0]
The lists after index elements replacements is : ['Gfg', 'is', 'best', 'is', 'Gfg', 'Gfg', 'Gfg', 'best', 'is', 'is', 'best', 'Gfg']
Time complexity: O(n), where n is the length of test_list2.
Auxiliary space: O(n), where n is the length of test_list2.
Please Login to comment...