Nathan Miller Nathan Miller - 1 year ago 138
Python Question

Get indexes for first occurrence of each element in a list Python

I have a list of about 90k elements (about 670 unique). I would like to get the indexes for the first occurrence of each value. I have just tried a list comprehension like this:

In: [["foo", "bar", "baz", "bar", "foo"].index(x) for x in ["foo", "bar", "baz", "bar", "foo"]]
Out: [0, 1, 2, 1, 0]

This works, but it takes a couple of minutes to run on my machine. What is a better (faster) way to do this?

Answer Source

Your question is very ambiguous but as i understood it you have many duplicate values and you just want to get the index of the first appearance for each. I would leverage sets like this:

my_list = ["foo", "bar", "baz", "bar", "foo"]

my_list_unique = set(my_list)
indexes = [(x, my_list.index(x)) for x in my_list_unique]
print(indexes)  # prints -> [('foo', 0), ('bar', 1), ('baz', 2)]

Note that the creation of a set in line 3 removes the duplicates so every entry in my_list_unique only exists once. This saves time when looking for the indexes. As far as the results go, it is a list of tuples where each tuple contains the string and the index in which it is first found in my_list

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download