on-internet
← Community

How to make new key-value pairs in a list of dictionaries using a list of values?

Stack Overflow ·

I have a list of dictionaries: d = [{'item': ' laptop ', 'category': 'electronics'}, {'item': 'DESK CHAIR', 'category': 'Furniture'}] I also have a list of values: test = [15, 8] This is what I am trying to achieve: d = [{'item': ' laptop ', 'category': 'electronics', 'stock': 15}, {'item': 'DESK CHAIR', 'category': 'Furniture', 'stock': 8}] I want to take each dictionary, add a 'stock' key and a value from the 'test' list, updating each dictionary with a new key-value pairing. I have tried: for record in d: record.update({'stock': i for i in test}) print(d) #output [{'item': ' laptop ', 'cate

Comments

Sign in or sign up to comment and upvote.

No comments yet.