Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
collections.OrderedDict
#1
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.
Reply
#2
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']})
Reply
#3
Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  cs.collections error DaveG 7 1,742 Apr-06-2022, 04:18 PM
Last Post: Larz60+
  Trying to understand how isinstance(values, collections.Iterable) work. quazirfan 7 4,181 Aug-10-2021, 08:10 AM
Last Post: snippsat
  How to escape OrderedDict as an argument? Mark17 2 2,018 Dec-23-2020, 06:47 PM
Last Post: Mark17
  extracting second values elements only in every keys in an OrderedDict glennford49 4 2,518 Nov-12-2020, 08:22 AM
Last Post: glennford49
  AttributeError: 'collections.OrderedDict' object has no attribute 'value_counts Kristenl2784 4 7,342 Jul-17-2020, 01:50 AM
Last Post: palladium
  AttributeError: module 'collections' has no attribute 'namedtuple' epgs1975 2 10,310 May-04-2020, 08:10 PM
Last Post: epgs1975
  KeyError in OrderedDict rberna 1 2,116 Apr-01-2020, 04:42 AM
Last Post: perfringo
  Problem with importing and using collections module pythomdummy 3 5,794 Nov-14-2019, 08:48 AM
Last Post: Gribouillis
  Please, advise collections for my task AlekseyPython 1 2,092 Sep-11-2019, 12:44 PM
Last Post: perfringo
  Iterating list of oredereddict for creating new list of ordereddict babypython 7 3,681 May-05-2019, 05:32 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020