Sometimes, while working with Python dictionaries, we can have a problem in which we need to assign list elements as a new key in dictionary. This task can occur in web development domain. Lets discuss certain ways in which this task can be performed.
Method #1 : Using zip() + loop
The combination of above functions can be used to solve this problem. In this, we combine list elements with dictionary using zip() and loop is used to combine iteration logic.
# Python3 code to demonstrate working of # Assign list items to Dictionary # Using zip() + loop # initializing list test_list = [{'Gfg' : 1, 'id' : 2 }, {'Gfg' : 4, 'id' : 4 }] # printing original list print("The original list is : " + str(test_list)) # initializing key new_key = 'best' # initializing list add_list = [12, 2] # Assign list items to Dictionary # Using zip() + loop res = [] for sub, val in zip(test_list, add_list): sub[new_key] = val res.append(sub) # printing result print("The modified dictionary : " + str(res)) |
The original list is : [{‘Gfg’: 1, ‘id’: 2}, {‘Gfg’: 4, ‘id’: 4}]
The modified dictionary : [{‘best’: 12, ‘Gfg’: 1, ‘id’: 2}, {‘best’: 2, ‘Gfg’: 4, ‘id’: 4}]
Method #2 : Using list comprehension + zip()
The combination of above functions can be used to solve this problem. In this, we perform the iteration of elements using list comprehension and hence a shorthand.
# Python3 code to demonstrate working of # Assign list items to Dictionary # Using list comprehension + zip() # initializing list test_list = [{'Gfg' : 1, 'id' : 2 }, {'Gfg' : 4, 'id' : 4 }] # printing original list print("The original list is : " + str(test_list)) # initializing key new_key = 'best' # initializing list add_list = [12, 2] # Assign list items to Dictionary # Using list comprehension + zip() res = [{**sub, new_key : ele} for sub, ele in zip(test_list, add_list)] # printing result print("The modified dictionary : " + str(res)) |
The original list is : [{‘Gfg’: 1, ‘id’: 2}, {‘Gfg’: 4, ‘id’: 4}]
The modified dictionary : [{‘best’: 12, ‘Gfg’: 1, ‘id’: 2}, {‘best’: 2, ‘Gfg’: 4, ‘id’: 4}]
Recommended Posts:
- Python - Common items Dictionary Value List
- Python - Extract dictionary items with List elements
- Python | Count number of items in a dictionary value that is a list
- Python - Assign Reversed Values in Dictionary
- Python - Assign values to initialized dictionary keys
- Python | Get first K items in dictionary
- Python - All possible items combination dictionary
- Dictionary Methods in Python | Set 1 (cmp(), len(), items()...)
- Python Dictionary | items() method
- Python - Dictionary items in value range
- Python - Convert dictionary items to values
- Python program to find the sum of all items in a dictionary
- Python - Frequency of unequal items in Dictionary
- Python | Delete items from dictionary while iterating
- Python - Append items at beginning of dictionary
- Python | Sort the items alphabetically from given dictionary
- Python - Remove K value items from dictionary nesting
- Python | Type conversion of dictionary items
- Python | Get items in sorted order from given dictionary
- Python - Column Mapped Tuples to dictionary items
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

