-1

Here is my example:

sample_dict = {'foo':[1], 'bar': [2]}
{el: sample_dict[el].append(3) for el in sample_dict.keys()}

it generates:

{'foo': None, 'bar': None}

while I am expecting this:

{'foo': [1,3], 'bar': [2,3]}

What am I missing?

user1700890
  • 7,144
  • 18
  • 87
  • 183
  • 4
    `append` returns `None`, not the list. – deceze Feb 21 '23 at 15:21
  • 4
    `{k: v + [3] for k, v in sample_dict.items()}` — But there's really little reason to use a dict comprehension here over a regular loop. – deceze Feb 21 '23 at 15:24
  • 1
    see also: https://stackoverflow.com/questions/5753597/is-it-pythonic-to-use-list-comprehensions-for-just-side-effects – JonSG Feb 21 '23 at 15:48

1 Answers1

0

If you need to do this way:

    {el: sample_dict[el]+[3] for el in sample_dict.keys()}

Generates expected output