Open In App

Python | Replace elements in second list with index of same element in first list

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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




# Python code to replace every element
# in second list with index of first element.
 
# List Initialization
Input1 = ['cut', 'god', 'pass']
Input2 = ['god', 'cut', 'cut', 'cut',
          'god', 'pass', 'cut', 'pass']
 
# List Initialization
Output = []
 
# Using iteration
for x in Input2:
    for y in Input1:
        if x == y:
            Output.append(Input1.index(y))
 
# Printing output
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




# Python code to replace every element
# in second list with index of first element.
 
# List initialization
Input1 = ['cut', 'god', 'pass']
 
# using enumerate
temp = {y:x for x, y in enumerate(Input1)}
 
# List initialization
Input2 = ['god', 'cut', 'cut', 'cut',
          'god', 'pass', 'cut', 'pass']
 
# Using list comprehension
Output = [temp.get(elem) for elem in Input2]
 
# Printing output
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




# Python code to replace every element
# in second list with index of first element.
 
# List initialization
Input1 = ['cut', 'god', 'pass']
 
# List initialization
Input2 = ['god', 'cut', 'cut', 'cut',
          'god', 'pass', 'cut', 'pass']
 
elem = {k: i for i, k in enumerate(Input1)}
Output = list(map(elem.get, Input2))
 
# Printing output
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):
  # Create a dictionary with keys from input1 and values as the indices
  dictionary = {input1[i]: i for i in range(len(input1))}
  # Use a list comprehension to replace each element in input2 with its corresponding index in the dictionary
  return [dictionary[elem] for elem in input2]
#initialization
Input1 = ['cut', 'god', 'pass']
Input2 = ['god', 'cut', 'cut', 'cut', 'god', 'pass', 'cut', 'pass']
Output = replace_elements(Input1, Input2)
#printing result
print(f"Input1: {Input1}")
print(f"Input2: {Input2}")
print(f"Output: {Output}")
#This code is contributed by Edula Vinay Kumar Reddy


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']
 
# Compile a regular expression pattern to match elements in Input1
pattern = re.compile("|".join(Input1))
 
# Use the re.finditer function to find all matches in Input2
matches = [m.group() for m in re.finditer(pattern, " ".join(Input2))]
 
# Replace elements in Input2 with their indices
Output = [Input1.index(x) for x in matches]
 
# Printing output
print("initial 2 list are")
print(Input1, "\n", Input2)
print("Second list after replacement is:", Output)
#This code is contributed by Vinay Pinjala.


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




# Python code to replace every element
# in second list with index of first element.
 
# List Initialization
Input1 = ['cut', 'god', 'pass']
Input2 = ['god', 'cut', 'cut', 'cut',
          'god', 'pass', 'cut', 'pass']
 
# List Initialization
Output = []
 
# Using enumerate() function
for i, x in enumerate(Input2):
    if x in Input1:
        Output.append(Input1.index(x))
 
# Printing output
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
Previous
Next
Share your thoughts in the comments
Similar Reads