List comprehension with if-else
Larry Martell
larry.martell at gmail.com
Wed Oct 28 14:31:18 EDT 2015
On Wed, Oct 28, 2015 at 12:36 PM, Zachary Ware
<zachary.ware+pylist at gmail.com> wrote:
> 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']]
Perfect. Thanks
More information about the Python-list
mailing list