Dan
Dan

Reputation: 135

Python loop in dictionary

I have two lists structured like:

list1 = [1,2]
list2 = [{'name':'foo',  'address':'bar'}, {'name':'foo1', 'address':'bar1'}]

I thing is that i want to append the list1 into list2 and create something like:

new_list = [{'name':'foo', 'address':'bar', 'num':'1'}, {'name':'foo1','address':'bar1', 'num':'2'}]

how can this be acheived?

Upvotes: 1

Views: 89

Answers (2)

Sede
Sede

Reputation: 61225

Simple for loop

for i, j in enumerate(list2):
    j.update({'num': list1[i]})

Upvotes: 1

Jim Dennis
Jim Dennis

Reputation: 17500

How about:

for n, d in zip(list1, list2):
    d['num']=n

Upvotes: 4

Related Questions