Kate
Kate

Reputation: 63

Indexing Lists of Strings

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

Answers (2)

Ivana Balazevic
Ivana Balazevic

Reputation: 148

Use list comprehension:

indices = [idx for (idx, element) in enumerate(test_list) if element == "1315"]

Upvotes: 1

RedVelvet
RedVelvet

Reputation: 1913

>>> ["foo", "bar", "baz"].index('bar')
1

With this istruction you can have the position of the element. This is the python reference of Lists

Upvotes: 0

Related Questions