Reputation: 471
I want to add to the first key of a dict the first element of a list, same for the second and so on. How can I do that?
d = {'a_': 1, 'b_': 2}
l = ['orange', 'apple']
I would like to get:
d = {'a_orange': 1, 'b_apple': 2}
I tried: A = [{f'{k}{items}': v for k, v in d.items() for j in l}
but this produce: ['a_orange', 'a_apple', 'a_orange', 'a_apple']
which is not what I am looking for.
Upvotes: 0
Views: 43
Reputation: 14083
Try dict comprehension with zip but this assumes that d
and l
are the same length and there is only one value for each key in d
d = {'a_': 1, 'b_': 2}
l = ['orange', 'apple']
{d[0]+l:d[1] for d,l in zip(d.items(),l)} # -> {'a_orange': 1, 'b_apple': 2}
Upvotes: 2