1# Example usage:
2your_list = ['The answer to', 'the ultimate question', 'of life',
3 'the universe', 'and everything', 'is 42']
4
5[idx for idx, elem in enumerate(your_list) if 'universe' in elem][0]
6--> 3 # The 0-based index of the first list element containing "universe"
1# Basic syntax using list comprehension:
2[i for i, n in enumerate(your_list) if n == condition][match_number]
3# Where:
4# - This setup makes a list of the indexes for list items that meet
5# the condition and then match_number returns the nth index
6# - enumerate() returns iterates through your_list and returns each
7# element (n) and index value (i)
8
9# Example usage:
10your_list = ['w', 'e', 's', 's', 's', 'z','z', 's']
11[i for i, n in enumerate(your_list) if n == 's'][0]
12--> 2
13# 2 is returned because it is the index of the first element that
14# meets the condition (being 's')
1# Basic syntax:
2list.index(element, start, end)
3# Where:
4# - Element is the item you're looking for in the list
5# - Start is optional, and is the list index you want to start at
6# - End is option, and is the list index you want to stop searching at
7
8# Note, Python is 0-indexed
9
10# Example usage:
11my_list = [1, 2, 3, 4, 5, 6, 7, 8, 42, 9, 10]
12my_list.index(42)
13--> 8