Python | Replace elements in second list with index of same element in first list
Given two lists of strings, where first list contains all elements of second list, the task is to replace every element in second list with index of elements in first list.
Method #1: Using Iteration
Python3
Input1 = [ 'cut' , 'god' , 'pass' ]
Input2 = [ 'god' , 'cut' , 'cut' , 'cut' ,
'god' , 'pass' , 'cut' , 'pass' ]
Output = []
for x in Input2:
for y in Input1:
if x = = y:
Output.append(Input1.index(y))
print ( "initial 2 list are" )
print (Input1, "\n" , Input2)
print ( "Second list after replacement is:" , Output)
|
Output:
initial 2 list are
['cut', 'god', 'pass']
['god', 'cut', 'cut', 'cut', 'god', 'pass', 'cut', 'pass']
Second list after replacement is: [1, 0, 0, 0, 1, 2, 0, 2]
Time Complexity : O(n*m)
Auxiliary Space : O(n+m), where n is length of Input1 list and m is length of Input2 list.
Method #2: Using List comprehension
Python3
Input1 = [ 'cut' , 'god' , 'pass' ]
temp = {y:x for x, y in enumerate (Input1)}
Input2 = [ 'god' , 'cut' , 'cut' , 'cut' ,
'god' , 'pass' , 'cut' , 'pass' ]
Output = [temp.get(elem) for elem in Input2]
print ( "initial 2 list are" )
print (Input1, "\n" , Input2)
print ( "Second list after replacement is:" , Output)
|
Output:
initial 2 list are
['cut', 'god', 'pass']
['god', 'cut', 'cut', 'cut', 'god', 'pass', 'cut', 'pass']
Second list after replacement is: [1, 0, 0, 0, 1, 2, 0, 2]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using map
Python3
Input1 = [ 'cut' , 'god' , 'pass' ]
Input2 = [ 'god' , 'cut' , 'cut' , 'cut' ,
'god' , 'pass' , 'cut' , 'pass' ]
elem = {k: i for i, k in enumerate (Input1)}
Output = list ( map (elem.get, Input2))
print ( "initial 2 list are" )
print (Input1, "\n" , Input2)
print ( "Second list after replacement is:" , Output)
|
Output:
initial 2 list are
['cut', 'god', 'pass']
['god', 'cut', 'cut', 'cut', 'god', 'pass', 'cut', 'pass']
Second list after replacement is: [1, 0, 0, 0, 1, 2, 0, 2]
Method #4: Using dictionary comprehension
This method uses a dictionary to store the indices of the elements in input1. It then uses a list comprehension to iterate through input2 and replace each element with its index in the dictionary. This is a simple and efficient way to solve the task.
Python3
def replace_elements(input1, input2):
dictionary = {input1[i]: i for i in range ( len (input1))}
return [dictionary[elem] for elem in input2]
Input1 = [ 'cut' , 'god' , 'pass' ]
Input2 = [ 'god' , 'cut' , 'cut' , 'cut' , 'god' , 'pass' , 'cut' , 'pass' ]
Output = replace_elements(Input1, Input2)
print (f "Input1: {Input1}" )
print (f "Input2: {Input2}" )
print (f "Output: {Output}" )
|
Output
Input1: ['cut', 'god', 'pass']
Input2: ['god', 'cut', 'cut', 'cut', 'god', 'pass', 'cut', 'pass']
Output: [1, 0, 0, 0, 1, 2, 0, 2]
Time complexity: O(n) where n is the length of input2
Auxiliary Space: O(n) to store the dictionary and output list
Method #5: Using Regular expressions
Python3
import re
Input1 = [ 'cut' , 'god' , 'pass' ]
Input2 = [ 'god' , 'cut' , 'cut' , 'cut' ,
'god' , 'pass' , 'cut' , 'pass' ]
pattern = re. compile ( "|" .join(Input1))
matches = [m.group() for m in re.finditer(pattern, " " .join(Input2))]
Output = [Input1.index(x) for x in matches]
print ( "initial 2 list are" )
print (Input1, "\n" , Input2)
print ( "Second list after replacement is:" , Output)
|
Output
initial 2 list are
['cut', 'god', 'pass']
['god', 'cut', 'cut', 'cut', 'god', 'pass', 'cut', 'pass']
Second list after replacement is: [1, 0, 0, 0, 1, 2, 0, 2]
Time complexity: O(n)
Auxiliary Space: O(n)
Method #6: Using enumerate() function
Use the built-in enumerate() function to iterate through the elements of Input2 along with their indices. Then, for each element in Input2, we can check if it exists in Input1 using the in operator. If it exists, we can append the index of the matching element in Input1 to the Output list.
Python3
Input1 = [ 'cut' , 'god' , 'pass' ]
Input2 = [ 'god' , 'cut' , 'cut' , 'cut' ,
'god' , 'pass' , 'cut' , 'pass' ]
Output = []
for i, x in enumerate (Input2):
if x in Input1:
Output.append(Input1.index(x))
print ( "initial 2 list are" )
print (Input1, "\n" , Input2)
print ( "Second list after replacement is:" , Output)
|
Output
initial 2 list are
['cut', 'god', 'pass']
['god', 'cut', 'cut', 'cut', 'god', 'pass', 'cut', 'pass']
Second list after replacement is: [1, 0, 0, 0, 1, 2, 0, 2]
Time Complexity: O(n^2), where n is the length of Input2.
Auxiliary Space: O(1), as we are only using the Output list to store the indices.
Last Updated :
16 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...