List comprehension with if-else

Zachary Ware zachary.ware+pylist at gmail.com
Wed Oct 28 12:36:00 EDT 2015


On Wed, Oct 28, 2015 at 11:25 AM, Larry Martell <larry.martell at gmail.com> wrote:
> I'm trying to do a list comprehension with an if and that requires an
> else, but in the else case I do not want anything added to the list.
>
> For example, if I do this:
>
> white_list = [l.control_hub.serial_number if l.wblist == wblist_enum['WHITE']  else None for l in wblist]

Switch the 'if' and the 'for':

   white_list = [l.control_hub.serial_number for l in wblist if
l.wblist == wblist_enum['WHITE']]

-- 
Zach



More information about the Python-list mailing list