Assuming we are working with smaller lists, it is as simple as:
>>> a = [1, 2, 9, 3, 8] >>> b = [1, 9, 1] >>> [a.index(item) for item in b] [0, 2, 0]
On large lists, this will become quite expensive.
(If there are duplicates, the first occurrence will always be what is indicated in the resulting list, if not set(b) <= set(a)
, you will get a ValueError).
source share