Reputation: 63
In Python, I have a list of strings that repeat the same string every 7th element:
test_list = ['1315', '1415', '1515', '1615', '1715', '1815','1915', '1315','1415', '1515', and so on].
I want to be able to only print out the indexes of one particular element such as all of the '1415' variables. I have been able to do this for the '1315'
one by doing test_list[::7]
, which will return all of the '1315'
in the list. I now want to do this for every other string in the list.
Upvotes: 1
Views: 103
Reputation: 148
Use list comprehension:
indices = [idx for (idx, element) in enumerate(test_list) if element == "1315"]
Upvotes: 1