I have two lists of equal length, as follows:
l1 = [{'a': 'foo'}, {'a': 'foo'}, {'a': 'bar'}, {'a': 'bar'}]
l2 = [{'b': 'foo'}, {'b': 'bar'}, {'b': 'foo'}, {'b': 'bar'}]
I would like to combine these lists so that the key and value of each index position is in the same dictionary in a third list, i.e.:
l3 = [{'a': 'foo', 'b': 'foo'}, {'a':'foo', 'b':'bar'}, {'a':'bar', 'b':'foo'}, {'a':'bar', 'b':'bar'}]
How can I achieve this? I have tried with the following code:
l3 = []
mydict = {}
index = 0
while index < len(l1):
mydict['a'] = l1[index]['a']
mydict['b'] = l2[index]['b']
l3.append(mydict)
index +=1
but this returns:
[{'a': 'bar', 'b': 'bar'}, {'a': 'bar', 'b': 'bar'}, {'a': 'bar', 'b': 'bar'}, {'a': 'bar', 'b': 'bar'}]