data = [get_job_efficiency_dict(job_id) for job_id in job_ids]...
filtered_data = list(filter(None, data))
"Loris Bennett" <loris.bennett@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]
. 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?
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?
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?
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.
Op 4/08/2022 om 13:51 schreef Loris Bennett:failure?
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
In that case your list will be all dictionaries and empty ones will be processed fast enough.
Antoon Pardon<antoon.pardon@vub.be> writes:
Op 4/08/2022 om 13:51 schreef Loris Bennett: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
Hi,Just wondering, why don't you return an empty dictionary in case of a failure?
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?
In that case your list will be all dictionaries and empty ones will be processed
fast enough.
than filtering out the empty dictionaries in one go, although obviously
one would need to benchmark that.
Op 5/08/2022 om 07:50 schreef Loris Bennett:
Antoon Pardon<antoon.pardon@vub.be> writes:
Op 4/08/2022 om 13:51 schreef Loris Bennett:When the list of dictionaries is processed, I would have to check each
Hi,Just wondering, why don't you return an empty dictionary in case of a
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?
failure?
In that case your list will be all dictionaries and empty ones will
be processed
fast enough.
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?
Falsebool( {} )
Truebool( { "a":1 } )
On 16/08/2022 00.56, Antoon Pardon wrote:
Op 5/08/2022 om 07:50 schreef Loris Bennett:'Truthiness':-
Antoon Pardon<antoon.pardon@vub.be> writes:I may be missing something but why would you have to check each element
Op 4/08/2022 om 13:51 schreef Loris Bennett:When the list of dictionaries is processed, I would have to check each
Hi,Just wondering, why don't you return an empty dictionary in case of a
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?
failure?
In that case your list will be all dictionaries and empty ones will
be processed
fast enough.
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.
to see if it is empty? What would go wrong if you just treated empty
dictionaries the same as non-empty directories?
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 296 |
Nodes: | 16 (2 / 14) |
Uptime: | 22:29:14 |
Calls: | 6,646 |
Calls today: | 1 |
Files: | 12,191 |
Messages: | 5,327,553 |