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 code to demonstrate # Replace index elements with elements in Other List # using list comprehension # Initializing lists test_list1 = [ 'Gfg' , 'is' , 'best' ]
test_list2 = [ 0 , 1 , 2 , 1 , 0 , 0 , 0 , 2 , 1 , 1 , 2 , 0 ]
# printing original lists print ("The original list 1 is : " + str (test_list1))
print ("The original list 2 is : " + str (test_list2))
# Replace index elements with elements in Other List # using list comprehension res = [test_list1[idx] for idx in test_list2]
# printing result print ("The lists after index elements replacements is : " + str (res))
|
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 code to demonstrate # Replace index elements with elements in Other List # using map() + lambda # Initializing lists test_list1 = [ 'Gfg' , 'is' , 'best' ]
test_list2 = [ 0 , 1 , 2 , 1 , 0 , 0 , 0 , 2 , 1 , 1 , 2 , 0 ]
# printing original lists print ("The original list 1 is : " + str (test_list1))
print ("The original list 2 is : " + str (test_list2))
# Replace index elements with elements in Other List # using map() + lambda res = list ( map ( lambda idx: test_list1[idx], test_list2))
# printing result print ("The lists after index elements replacements is : " + str (res))
|
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 code to demonstrate # Replace index elements with elements in Other List # using numpy.take() # import numpy import numpy as np
# Initializing lists test_list1 = [ 'Gfg' , 'is' , 'best' ]
test_list2 = [ 0 , 1 , 2 , 1 , 0 , 0 , 0 , 2 , 1 , 1 , 2 , 0 ]
# printing original lists print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
# Replace index elements with elements in Other List # using numpy.take() res = np.take(test_list1, test_list2)
# printing result 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
# Initializing lists test_list1 = [ 'Gfg' , 'is' , 'best' ]
test_list2 = [ 0 , 1 , 2 , 1 , 0 , 0 , 0 , 2 , 1 , 1 , 2 , 0 ]
# printing original lists print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
# Create a dictionary to map indices to elements in test_list1 index_map = {index: element for index, element in enumerate (test_list1)}
# Replace index elements with elements in Other List using a for loop res = [index_map[idx] for idx in test_list2]
# printing result print ( "The lists after index elements replacements is : " + str (res))
#This code is contributed by Vinay Pinjala. |
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.
import pandas as pd
# Initializing the two lists test_list1 = [ 'Gfg' , 'is' , 'best' ]
test_list2 = [ 0 , 1 , 2 , 1 , 0 , 0 , 0 , 2 , 1 , 1 , 2 , 0 ]
# Creating a dataframe with one column ('a') containing the values from test_list1 df = pd.DataFrame({ 'a' : test_list1})
# Extracting the values from test_list1 corresponding to the indices in test_list2 # by using the loc[] function of the dataframe and converting the result to a list res = df.loc[test_list2, 'a' ].tolist()
# Printing the resulting list 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.
# Initializing lists test_list1 = [ 'Gfg' , 'is' , 'best' ]
test_list2 = [ 0 , 1 , 2 , 1 , 0 , 0 , 0 , 2 , 1 , 1 , 2 , 0 ]
# printing original lists print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
# Using zip() function res = [test_list1[i] for i in test_list2]
# printing result print ( "The lists after index elements replacements is : " + str (res))
#This code is contributed by Vinay Pinjala. |
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.