Search code examples
pythonlist-comprehension

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:

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}