add new key value for every dict of dict with list of dict
NickName:MrOldSir Ask DateTime:2022-04-04T04:01:55

add new key value for every dict of dict with list of dict

I have dict of dict:

a = {
    'a': {'id': 1},
    'b': {'id': 1},
    'c': {'id': 1}
}

And list of dict:

b = [{'word': 'foo'}, {'word': 'baz'}, {'word': 'bar'}]

I need update every dict from a to every value key word from b

Example:

a = {
    'a': {'id': 1, 'word': foo},
    'b': {'id': 1, 'word': baz},
    'c': {'id': 1, 'word': bar}
}

i try do it by the next way:

[
a.update(
    {
        'word': i
    }
)
for a in a.values()
for i in [
    df['word'] for df in b
    ]
]

but i have all new keys with last word bar:

    a = {
    'a': {'id': 1, 'word': bar},
    'b': {'id': 1, 'word': bar},
    'c': {'id': 1, 'word': bar}
}

How i can solve it?

Copyright Notice:Content Author:「MrOldSir」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/71729433/add-new-key-value-for-every-dict-of-dict-with-list-of-dict

Answers
evanstjabadi 2022-04-03T20:23:52

I like the first answer...but along with your thinking, you just needed to get an index from enumerating the dictionary and use that to index the list.\na = {"a": {"id": 1}, "b": {"id": 1}, "c": {"id": 1}}\n\nb = [{"word": "foo"}, {"word": "baz"}, {"word": "bar"}]\n\nc = [\n value.update({"word": b[index]["word"]})\n for index, (key, value) in enumerate(a.items())\n]\n\nprint(a)\n{\n "a": {"id": 1, "word": "foo"},\n "b": {"id": 1, "word": "baz"},\n "c": {"id": 1, "word": "bar"},\n}\n",


chepner 2022-04-03T20:11:47

Use zip to iterate over a and b concurrently:\nfor k, d in zip(a, b):\n a[k].update(d)\n\nResult:\n{'a': {'id': 1, 'word': 'foo'}, \n 'b': {'id': 1, 'word': 'baz'},\n 'c': {'id': 1, 'word': 'bar'}}\n\nYou can also use a.values() as one of the arguments to zip:\nfor d1, d2 in zip(a.values(), b):\n d1.update(d2)\n",


More about “add new key value for every dict of dict with list of dict” related questions

add new key value for every dict of dict with list of dict

I have dict of dict: a = { 'a': {'id': 1}, 'b': {'id': 1}, 'c': {'id': 1} } And list of dict: b = [{'word': 'foo'}, {'word&#x

Show Detail

How to add a new key pair in list of dict without modifying actual list of dict?

I have a list of dict commandsInWord . I am trying to filter this list by searching for a command Name in the dicts. It gives me a list of dicts commandinWordExists in which the command exists. ...

Show Detail

Python attach list of dict to another dict as new key

I have two lists. First one (list a) contain lists of dicts and every list represent comments from the specific post. They all have the same 'id' value. Second list (list b) contain dicts only and ...

Show Detail

add a second value to a key (dict) python

I need to make sure I don't have more than one of the same keys, if so, leave the first one and add their value (make it a list) to the existing key this is what I tried: my_dict = {1: "A", 2: "B"...

Show Detail

Copy nested dict in list to new dict

Nested dict in list looks like: lista = [{stuff1: {key1: value1, key2: value2}}, {stuff2: {keya: valuea, keyb: valueb}}] How would i go about copying element 0 out of that list so tha...

Show Detail

Create a new list of dict, from a dict with a key that has a list value

I have a dict with one of the keys have a value of list example = {"a":1,"b":[1,2]} I am trying to unpack example["b"] and create a list of the same dict with separate

Show Detail

Create a new dict with means from a dict of dicts

I have a dictionary like: dict = {'abc':{'a':1,'b':2,'c':3}, 'bla':{'a':0,'b':9}, 'ind':{'b':3,'c':4} } And I want to use li

Show Detail

list in the value of a dict python

I am trying to write a function that contains a dict in a way that the value of every key looks in the following order: for exmple if the function got the following list: list = [(a,b),(a,c)(f,q)...

Show Detail

how to return certain key, value from dict and print new dict

My assessment is that I need to return the students with a grade higher than 9 in the dict_studenten_cijfers dictionary. Right now I am printing the new result but the assessment is return. I tried...

Show Detail

Remove dict from list of dict if key and value doesnt match with other dict

I have a list of dictionaries and I want to filter it by values from other dictionary. orig_list = [{"name":"Peter","last_name":"Wick","mail":"[email protected]","number":"1

Show Detail