I have some list like this:
lines = [
"line",
"subline2",
"subline4",
"line",
]
And I want to take list of index of lines which starts with some substring.
I use this approach:
starts = [n for n, l in enumerate(lines) if l.startswith('sub')]
but maybe anybody knows more beautiful approach?
While I like your approach, here is another one that handles identical entries in lines
correctly (i.e. similar to the way your sample code does), and has comparable performance, also for the case that the length of lines
grows:
starts = [i for i in range(len(lines)) if lines[i].startswith('sub')]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With