Possible Duplicate:
Return the first item in a list matching a condition
How to easily grab the "matching" list element using list comprehension? e.g. I've a list
and I'm looking for a element that starts with a a certain string. That's easy to do:
>>> lines = ['AHP Buildlife number', 'NIT-Version-Default-v0.16.0', 'E_release v2.3.14.0']
>>> [ x.strip() for x in lines if x.startswith('NIT-Version-Default') ]
['NIT-Version-Default-v0.16.0']
But how can I do the same from in a if
statement so that the matching list-element can be used for further processing; some thing like this:
>>> if [ x.strip() for x in lines if x.startswith('NSS-Checkin-Default') ]:
... ver = x.split('-')[-1].strip()
... print ver
So, that it return v0.16.0
as the version number. This obviously doesn't work but hopefully you get the idea what I'm trying to do. Any idea how to do that? Cheers!!
PS. You are welcome to improve the question or the title.