I
I
Ivan_R22020-09-16 12:08:08
Python
Ivan_R2, 2020-09-16 12:08:08

What is the least resource intensive way to add a new element to each dictionary of a list in Python?

Hello!
I have a list of dictionaries, let's say

d = [{'id': 1}, {'id': 5}, {'id': 77}, {'id': 100}]

What would be the least resource-intensive way to add the key 'my_key' to each dictionary of the list with the value , where i is the element of the dictionary? 'my_string' + str(i['id'])

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-09-16
@bacon

Iterating and adding, it's more like you don't know how to do it at all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question