Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: 'as' keyword in list comprehension?

I know this won't work but you guys get the idea.

c = [m.split('=')[1] as a for m in matches if a != '1' ]

Is there a way to archive this? If you use a list comprehension like

c = [m.split('=')[1] as a for m in matches if m.split('=')[1] != '1' ]

two lists will be build from the split, right?

like image 385
LarsVegas Avatar asked Jun 04 '14 06:06

LarsVegas


1 Answers

You can use use a generator expression inside the list comprehension:

c = [a for a in (m.split('=')[1] for m in matches) if a != '1']
like image 88
Ashwini Chaudhary Avatar answered Sep 28 '22 00:09

Ashwini Chaudhary