Exclude 'None' from list comprehension of dicts

Loris Bennett loris.bennett at fu-berlin.de
Thu Aug 4 08:58:28 EDT 2022


ram at zedat.fu-berlin.de (Stefan Ram) writes:

> "Loris Bennett" <loris.bennett at fu-berlin.de> writes:
>>data = [get_job_efficiency_dict(job_id) for job_id in job_ids]
> ...
>>filtered_data = list(filter(None, data))
>
>   You could have "get_job_efficiency_dict" return an iterable
>   that yields either zero dictionaries or one dictionary.
>   For example, a list with either zero entries or one entry.
>
>   Then, use "itertools.chain.from_iterable" to merge all those
>   lists with empty lists effectively removed. E.g.,
>
> print( list( itertools.chain.from_iterable( [[ 1 ], [], [ 2 ], [ 3 ]])))
>
>   will print
>
> [1, 2, 3]

'itertool' is a bit of a blind-spot of mine, so thanks for pointing that
out. 

>   . Or, consider a boring old "for" loop:
>
> data = []
> for job_id in job_ids:
>     dictionary = get_job_efficiency_dict( job_id )
>     if dictionary:
>         data.append( dictionary )
>
>   . It might not be "elegant", but it's quite readable to me.

To me to.  However, 'data' can occasionally consist of many 10,000s of
elements.  Would there be a potential performance problem here?  Even if
there is, it wouldn't be so bad, as the aggregation of the data is not
time-critical and only occurs once a month.  Still, I wouldn't want the
program to be unnecessarily inefficient.

Cheers,

Loris

-- 
This signature is currently under construction.


More information about the Python-list mailing list