index()
will just give the first occurrence of an item in a list. Is there a neat trick which returns all indices in a list?
Answer
You can use a list comprehension:
indices = [i for i, x in enumerate(my_list) if x == "whatever"]
index()
will just give the first occurrence of an item in a list. Is there a neat trick which returns all indices in a list?
Answer
You can use a list comprehension:
indices = [i for i, x in enumerate(my_list) if x == "whatever"]
Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...
No comments:
Post a Comment