Python Forum
collections.OrderedDict - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: collections.OrderedDict (/thread-28214.html)



collections.OrderedDict - Bert123456 - Jul-09-2020

Hi, I have an object which looks like that:

a = ([('AAA', {'xxx': Value1, 'yyy': Value2})], [BBB, {'xxx': Value3, 'yyy': Value4}])
and I would like to get a list of all vlaues for specific item.
For exaMPLE
list all values for 'xxx' -> Value1, Value3)
or
list
all values for 'yyy' --> Value2, Value4

Does anone has an idea ?

Thanks a lot.


RE: collections.OrderedDict - Yoriz - Jul-09-2020

Your object is not very consistent
using a defaultdict
import collections

items = (['AAA', {'xxx': 'Value1', 'yyy': 'Value2'}], ['BBB', {'xxx': 'Value3', 'yyy': 'Value4'}])

itemsdict = collections.defaultdict(list)
for item in items:
    for item in item:
        if isinstance(item, dict):
            for key, value in item.items():
                itemsdict[key].append(value)

print(itemsdict)
Output:
defaultdict(<class 'list'>, {'xxx': ['Value1', 'Value3'], 'yyy': ['Value2', 'Value4']})



RE: collections.OrderedDict - Bert123456 - Jul-09-2020

Thanks