Op 5/08/2022 om 07:50 schreef Loris Bennett:
Antoon Pardon<antoon.par...@vub.be>  writes:

Op 4/08/2022 om 13:51 schreef Loris Bennett:
Hi,

I am constructing a list of dictionaries via the following list
comprehension:

    data = [get_job_efficiency_dict(job_id) for job_id in job_ids]

However,

    get_job_efficiency_dict(job_id)

uses 'subprocess.Popen' to run an external program and this can fail.
In this case, the dict should just be omitted from 'data'.

I can have 'get_job_efficiency_dict' return 'None' and then run

    filtered_data = list(filter(None, data))

but is there a more elegant way?
Just wondering, why don't you return an empty dictionary in case of a failure?
In that case your list will be all dictionaries and empty ones will be processed
fast enough.
When the list of dictionaries is processed, I would have to check each
element to see if it is empty.  That strikes me as being less efficient
than filtering out the empty dictionaries in one go, although obviously
one would need to benchmark that.

I may be missing something but why would you have to check each element
to see if it is empty? What would go wrong if you just treated empty
dictionaries the same as non-empty directories?

--
Antoon Pardon.
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to