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
# 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) |
chevron_right
filter_none
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 #2: Using List comprehension
# Python code to replace every element # in second list with index of first element. # List initialization Input1 = ['cut', 'god', 'pass'] # using enumeate 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) |
chevron_right
filter_none
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 #3 : Using map
# 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) |
chevron_right
filter_none
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]
Recommended Posts:
- Python | Add list elements with a multi-list based on index
- Python | Replace list elements with its ordinal number
- Python | Print list after removing element at given index
- Python | Equate two list index elements
- Python | Index of Non-Zero elements in Python list
- Python | Get elements till particular element in list
- Python | Sort list of list by specified index
- Python | Count the elements in a list until an element is a Tuple
- Python | Indices list of matching element from other list
- Python | Sorting list of lists with similar list elements
- Python | Merge List with common elements in a List of Lists
- Python | Move one list element to another list
- Python | Indices of sorted list of list elements
- Python | Maximum sum of elements of list in a list of lists
- Python | Replace sublist with other in list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



