Python Tips & Tricks

If you need to find the index of an item in a list that matches e.g. a part of a string you’re searching for…

indices = [i for i, s in enumerate(lines) if 'gs4row' in s]

This will return a list of indices, telling you where in the list “lines” the string “gs4row” occurs.

Leave a Reply

Your email address will not be published. Required fields are marked *