Answers for "python get index of every nth element in list"

2

python find index of nth occurrence in list

# Basic syntax using list comprehension:
[i for i, n in enumerate(your_list) if n == condition][match_number]
# Where:
#	- This setup makes a list of the indexes for list items that meet
#		the condition and then match_number returns the nth index
#	- enumerate() returns iterates through your_list and returns each
#		element (n) and index value (i)

# Example usage:
your_list = ['w', 'e', 's', 's', 's', 'z','z', 's']
[i for i, n in enumerate(your_list) if n == 's'][0]
--> 2
# 2 is returned because it is the index of the first element that
#	meets the condition (being 's')
Posted by: Guest on December-15-2020

Code answers related to "python get index of every nth element in list"

Python Answers by Framework

Browse Popular Code Answers by Language