Reputation: 177
I want to replace the following for loop into a one-liner dict comprehension
my_dict={'i':([3,4,5,6]), 'j':[10,20,30,40]}
for key, value in my_dict.items():
zeros=[0]*2
zeros.extend(value)
my_dict[key] = zeros
into:
{key:([0]*2).extend(value) for key, value in my_dict.items()}
However that does not seem to work. I have also tried:
{key:value.insert(0, [0,0]) for key, value in my_dict.items()}
without success.
Desired output is
{'i': [0, 0, 3, 4, 5, 6], 'j': [0, 0, 10, 20, 30, 40]}
Upvotes: -2
Views: 101
Reputation: 27515
list.extend
and list.insert
both return None
. What you could do is just append the values list to a list of zero's:
{key: [0, 0] + value for key, value in my_dict.items()}
Upvotes: 2