Python Add custom values key in List of dictionaries (original) (raw)

The task of adding custom values as keys in a list of dictionaries involves inserting a new key-value pair into each dictionary within the list. In Python, dictionaries are mutable, meaning the key-value pairs can be modified or added easily. When working with a list of dictionaries, the goal is to append a new key to each dictionary in the list with a corresponding value.

For example, given a list of dictionaries a = [{‘Gfg’: 6, ‘is’: 9, ‘best’: 10}, {‘Gfg’: 8, ‘is’: 11, ‘best’: 19}] and a new key k = ‘CS’ with corresponding values from the list b = [6, 7], we can iterate over the list and add the new key-value pair to each dictionary. After this operation, each dictionary in the list will have a new key ‘CS’ with its corresponding value from b. The output will be [{‘Gfg’: 6, ‘is’: 9, ‘best’: 10, ‘CS’: 6}, {‘Gfg’: 8, ‘is’: 11, ‘best’: 19, ‘CS’: 7} .

Using list comprehension

List comprehension is the most efficient way as it allows us to iterate over the list of dictionaries and add a new key-value pair to each dictionary concisely. It’s widely preferred for its readability and performance.

Python `

a = [{"Gfg": 6, "is": 9, "best": 10}, {"Gfg": 8, "is": 11, "best": 19}, {"Gfg": 2, "is": 16, "best": 10}, {"Gfg": 12, "is": 1, "best": 8}, {"Gfg": 22, "is": 6, "best": 8}]

k = "CS" # initializing key b = [6, 7, 4, 3, 9] # initializing append list

a = [{**ele, k: b[idx]} for idx, ele in enumerate(a)]

print(a)

`

**Output

[{'Gfg': 6, 'is': 9, 'best': 10, 'CS': 6}, {'Gfg': 8, 'is': 11, 'best': 19, 'CS': 7}, {'Gfg': 2, 'is': 16, 'best': 10, 'CS': 4}, {'Gfg': 12, 'is': 1, 'best': 8, 'CS': 3},
{'Gfg': 22, 'is': 6, 'best': 8, 'CS': 9}]

**Explanation:

Table of Content

Using map()

map() applies a function defined using lambda to each dictionary in the list. It works similarly to list comprehension but may be less familiar for some.

Python `

a = [{"Gfg": 6, "is": 9, "best": 10}, {"Gfg": 8, "is": 11, "best": 19}, {"Gfg": 2, "is": 16, "best": 10}, {"Gfg": 12, "is": 1, "best": 8}, {"Gfg": 22, "is": 6, "best": 8}]

k = "CS" # initializing key b = [6, 7, 4, 3, 9] # initializing append list

a = list(map(lambda d, idx: {**d, k: b[idx]}, a, range(len(a)))) print(a)

`

**Output

[{'Gfg': 6, 'is': 9, 'best': 10, 'CS': 6}, {'Gfg': 8, 'is': 11, 'best': 19, 'CS': 7}, {'Gfg': 2, 'is': 16, 'best': 10, 'CS': 4}, {'Gfg': 12, 'is': 1, 'best': 8, 'CS': 3},
{'Gfg': 22, 'is': 6, 'best': 8, 'CS': 9}]

**Explanation:

Using zip()

While this approach can also work, it’s less efficient compared to the other methods. It involves pairing the dictionaries with the values and then constructing new dictionaries, which can introduce overhead.

Python `

a = [{"Gfg": 6, "is": 9, "best": 10}, {"Gfg": 8, "is": 11, "best": 19}, {"Gfg": 2, "is": 16, "best": 10}, {"Gfg": 12, "is": 1, "best": 8}, {"Gfg": 22, "is": 6, "best": 8}]

k = "CS" # initializing key b = [6, 7, 4, 3, 9] # initializing append list

a = [dict(list(d.items()) + [(k, v)]) for d, v in zip(a, b)]

print(a)

`

**Output

[{'Gfg': 6, 'is': 9, 'best': 10, 'CS': 6}, {'Gfg': 8, 'is': 11, 'best': 19, 'CS': 7}, {'Gfg': 2, 'is': 16, 'best': 10, 'CS': 4}, {'Gfg': 12, 'is': 1, 'best': 8, 'CS': 3},
{'Gfg': 22, 'is': 6, 'best': 8, 'CS': 9}]

**Explanation:

Using loop

Loop is the traditional approach, where we loop through the list and use the update() method to add the new key-value pair. This method is simple and works well if we’re more comfortable with straightforward programming style.

Python `

a = [{"Gfg": 6, "is": 9, "best": 10}, {"Gfg": 8, "is": 11, "best": 19}, {"Gfg": 2, "is": 16, "best": 10}, {"Gfg": 12, "is": 1, "best": 8}, {"Gfg": 22, "is": 6, "best": 8}]

k = "CS" b = [6, 7, 4, 3, 9]

for idx, ele in enumerate(a): ele.update({k: b[idx]})

print(a)

`

**Output

[{'Gfg': 6, 'is': 9, 'best': 10, 'CS': 6}, {'Gfg': 8, 'is': 11, 'best': 19, 'CS': 7}, {'Gfg': 2, 'is': 16, 'best': 10, 'CS': 4}, {'Gfg': 12, 'is': 1, 'best': 8, 'CS': 3},
{'Gfg': 22, 'is': 6, 'best': 8, 'CS': 9}]

**Explanation: