Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

List comprehension for a dictionary?

I have a quite simple problem, but I am not able to solve it.

I have to pass a dictionary to a function like this:

session.run(
    output_names=[_input.name for _input in session.get_outputs()],
    input_feed={
        "input_ids": inputs["input_ids"],
        "attention_mask": inputs["attention_mask"],
        "token_type_ids": inputs["token_type_ids"],
    },
)

For the output_names it´s quite easy, but how can I do this for the input_feed? Is it possible to convert this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

empty = {}
for _input in inputs:
    empty[_input] = _input

into a oneliner?

>Solution :

Dictionary comprehension is indeed a thing and works like you might expect. For example:

empty = {x: x for x in inputs}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading