python – Return the index of a list element given part of its value

Question:

For example, there is a list:

['Город Москва', 'Город Киев', 'Город Харьков', 'Город Краснодар']

How to return the index of an element given its value? For example Kharkov.

Answer:

In [33]: items
Out[33]: ['Город Москва', 'Город Киев', 'Город Харьков', 'Город Краснодар']

face-to-face solution:

In [34]: [i for i,x in enumerate(items) if 'Харьков' in x][0]
Out[34]: 2
Scroll to Top