Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Adding Custom Values Key in a List of Dictionaries in Python
In this article, we will learn how to add a custom key with corresponding values to a list of dictionaries in Python. This is a common task when working with structured data.
Dictionaries in Python are collections of unordered items stored in the form of key-value pairs inside curly braces ?
dictionary = {"key": "value", "name": "John"}
print(dictionary)
{'key': 'value', 'name': 'John'}
Suppose we have a list of dictionaries called dict_list and a key new_key, with its values provided in a separate list value_list. The goal is to add new_key to each dictionary in dict_list, assigning it the corresponding value from value_list.
Example Scenario
# Initial list of dictionaries
dict_list = [
{'name': 'John', 'location': 'Hyderabad'},
{'name': 'Jane', 'location': 'Bangalore'}
]
# Key and values to add
new_key = 'age'
value_list = [22, 23]
print("Original:", dict_list)
print("Adding key:", new_key)
print("Values to add:", value_list)
Original: [{'name': 'John', 'location': 'Hyderabad'}, {'name': 'Jane', 'location': 'Bangalore'}]
Adding key: age
Values to add: [22, 23]
We can add custom key-value pairs to each dictionary using several approaches ?
Using enumerate() Function with For Loop
The enumerate() function adds a counter to each item of an iterable. We use it to access both the index and dictionary simultaneously ?
# Initializing data
dict_list = [
{'name': 'John', 'location': 'Hyderabad'},
{'name': 'Jane', 'location': 'Bangalore'}
]
new_key = 'age'
value_list = [22, 23]
# Adding new key-value pair using enumerate
for i, d in enumerate(dict_list):
d[new_key] = value_list[i]
print(dict_list)
[{'name': 'John', 'location': 'Hyderabad', 'age': 22}, {'name': 'Jane', 'location': 'Bangalore', 'age': 23}]
Using List Comprehension with enumerate()
List comprehension provides a concise way to create new lists. We use dictionary unpacking (**dic) to preserve existing keys ?
# Initializing data
dict_list = [
{'name': 'John', 'location': 'Hyderabad'},
{'name': 'Jane', 'location': 'Bangalore'}
]
new_key = 'age'
value_list = [22, 23]
# Creating new list with added key-value pairs
dict_list = [{**dic, new_key: value_list[idx]} for idx, dic in enumerate(dict_list)]
print(dict_list)
[{'name': 'John', 'location': 'Hyderabad', 'age': 22}, {'name': 'Jane', 'location': 'Bangalore', 'age': 23}]
Using List Comprehension with zip()
The zip() function pairs elements from multiple iterables by index position. This allows us to combine dictionaries and values directly ?
# Initializing data
dict_list = [
{'name': 'John', 'location': 'Hyderabad'},
{'name': 'Jane', 'location': 'Bangalore'}
]
new_key = 'age'
value_list = [22, 23]
# Adding new key-value pair using zip()
dict_list = [{**dic, new_key: val} for dic, val in zip(dict_list, value_list)]
print(dict_list)
[{'name': 'John', 'location': 'Hyderabad', 'age': 22}, {'name': 'Jane', 'location': 'Bangalore', 'age': 23}]
Using the map() Function
The map() function applies a function to each item in an iterable. We use a lambda function to add the new key-value pair ?
# Initializing data
dict_list = [
{'name': 'John', 'location': 'Hyderabad'},
{'name': 'Jane', 'location': 'Bangalore'}
]
new_key = 'age'
value_list = [22, 23]
# Using map() with lambda to add key-value pairs
dict_list = list(map(
lambda dic, val: {**dic, new_key: val},
dict_list,
value_list
))
print(dict_list)
[{'name': 'John', 'location': 'Hyderabad', 'age': 22}, {'name': 'Jane', 'location': 'Bangalore', 'age': 23}]
Comparison
| Method | Modifies Original? | Best For |
|---|---|---|
enumerate() + for loop |
Yes | In-place modification |
enumerate() + list comprehension |
No | Creating new list |
zip() + list comprehension |
No | Clean, readable syntax |
map() |
No | Functional programming style |
Conclusion
Use zip() with list comprehension for the cleanest syntax when creating new dictionaries. Use enumerate() with a for loop when modifying dictionaries in-place. The map() function offers a functional programming approach for complex transformations.
