Home > front end >  iterating over a list of dictionaries with a for loop
iterating over a list of dictionaries with a for loop

Time:04-09

enter image description here I have a variable that looks like this, it contains multiple lists and each list has multiple dictionaries. what i need to do now is:

  1. combine the lists into 1 big list
  2. if 2 dictionaries have the same key i need to combine them(keep 1 of the keys and add their values)

i know i need to use a for loop but how do i reference dictionaries inside a list and how do i refernce the lists stored in the variable?

i tried doing something like this:

        for list in bigram_lists:
            for list1 in bigram_lists:
                list.append(list1)

it gives back the error that dict object has no attribute append help would be appreciated

CodePudding user response:

import ast

x = "[{'a': 1850}, {'b': 397}, {'c': 811}, {'d': 990}, {'e': 3198}, {'f': 605}, {'g': 435}, {'h': 1339}, {'i': 1904}, {'j': 59}, {'k': 138}, {'l': 946}, {'m': 652}, {'n': 1691}, {'o': 1813}, {'p': 510}, {'q': 13}, {'r': 1469}, {'s': 1695}, {'t': 2322}, {'u': 516}, {'v': 285}, {'w': 353}, {'x': 49}, {'y': 393}, {'z': 23}] [{'a': 3815}, {'b': 716}, {'c': 1989}, {'d': 1904}, {'e': 5429}, {'f': 908}, {'g': 836}, {'h': 1902}, {'i': 3340}, {'j': 42}, {'k': 148}, {'l': 1818}, {'m': 1156}, {'n': 3782}, {'o': 3365}, {'p': 992}, {'q': 98}, {'r': 2683}, {'s': 3125}, {'t': 3708}, {'u': 1123}, {'v': 335}, {'w': 399}, {'x': 153}, {'y': 706}, {'z': 85}] [{'a': 5087}, {'b': 823}, {'c': 1949}, {'d': 2366}, {'e': 6904}, {'f': 1322}, {'g': 1128}, {'h': 2756}, {'i': 3754}, {'j': 138}, {'k': 346}, {'l': 2709}, {'m': 1618}, {'n': 4391}, {'o': 4675}, {'p': 1321}, {'q': 74}, {'r': 3681}, {'s': 3554}, {'t': 5438}, {'u': 1658}, {'v': 519}, {'w': 1012}, {'x': 128}, {'y': 718}, {'z': 53}]"

strs = x.replace(']','],')[:-1]
strs = "["   strs   "]"
listOfLists = ast.literal_eval(strs)

finalDict = {}
for ls in listOfLists:
    for dct in ls:
        if (list(dct.keys())[0]) in finalDict:
            finalDict[list(dct.keys())[0]]  = dct[list(dct.keys())[0]]
        else:
            finalDict[list(dct.keys())[0]] = dct[list(dct.keys())[0]]

print(finalDict)

gives you

{'a': 10752, 'b': 1936, 'c': 4749, 'd': 5260, 'e': 15531, 'f': 2835, 'g': 2399, 'h': 5997, 'i': 8998, 'j': 239, 'k': 632, 'l': 5473, 'm': 3426, 'n': 9864, 'o': 9853, 'p': 2823, 'q': 185, 'r': 7833, 's': 8374, 't': 11468, 'u': 3297, 'v': 1139, 'w': 1764, 'x': 330, 'y': 1817, 'z': 161}

CodePudding user response:

Working with x as a list of lists, I created a dictionary with multiple keys, that you can split if you want later, but each key has the addition of the same key in each list :

result = {}
for sublist in x:
    for elem in sublist:
        for key, value in elem.items():
            if key not in result:
                result[key] = value
            else:
                result[key]  = value
>>> print(result)
{'a': 10752, 'b': 1936, 'c': 4749, 'd': 5260, 'e': 15531, 'f': 2835, 'g': 2399, 'h': 5997, 'i': 8998, 'j': 239, 'k': 632, 'l': 5473, 'm': 3426, 'n': 9864, 'o': 9853, 'p': 2823, 'q': 185, 'r': 7833, 's': 8374, 't': 11468, 'u': 3297, 'v': 1139, 'w': 1764, 'x': 330, 'y': 1817, 'z': 161}

CodePudding user response:

Having corrected the x input as a list of lists:

x = [[{'a': 1850}, {'b': 397}, {'c': 811}, {'d': 990}, {'e': 
3198}, {'f': 605}, {'g': 435}, {'h': 1339}, {'i': 1904}, {'j': 
59}, {'k': 138}, {'l': 946}, {'m': 652}, {'n': 1691}, {'o': 
1813}, {'p': 510}, {'q': 13}, {'r': 1469}, {'s': 1695}, {'t': 
2322}, {'u': 516}, {'v': 285}, {'w': 353}, {'x': 49}, {'y': 393}, 
{'z': 23}],

[{'a': 3815}, {'b': 716}, {'c': 1989}, {'d': 1904}, {'e': 5429}, 
{'f': 908}, {'g': 836}, {'h': 1902}, {'i': 3340}, {'j': 42}, 
{'k': 148}, {'l': 1818}, {'m': 1156}, {'n': 3782}, {'o': 3365}, 
{'p': 992}, {'q': 98}, {'r': 2683}, {'s': 3125}, {'t': 3708}, 
{'u': 1123}, {'v': 335}, {'w': 399}, {'x': 153}, {'y': 706}, 
{'z': 85}],

[{'a': 5087}, {'b': 823}, {'c': 1949}, {'d': 2366}, {'e': 6904}, 
{'f': 1322}, {'g': 1128}, {'h': 2756}, {'i': 3754}, {'j': 138}, 
{'k': 346}, {'l': 2709}, {'m': 1618}, {'n': 4391}, {'o': 4675}, 
{'p': 1321}, {'q': 74}, {'r': 3681}, {'s': 3554}, {'t': 5438}, 
{'u': 1658}, {'v': 519}, {'w': 1012}, {'x': 128}, {'y': 718}, 
{'z': 53}]]

this:

R=[]
for ld in x:
    result = {}
    for d in ld:
        result.update(d)
    R.append(result)

D = dict.fromkeys(R[0].keys(), 0)

for d in R:
    for k in R[0].keys():
        D[k] =d[k]

will give you the answer you wanted.

  • Related