OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How to combine complex dicts in list of complex dicts where key appears multiple times

  • Thread starter Thread starter Bob
  • Start date Start date
B

Bob

Guest
I have a list of dicts, where each dict refers to a team and its assets.

e.g.

Code:
list_of_dicts = [
    {
        "team": "X",
        "description": "A description of X",
        "types": [
            {
                "type": "TYPEA",
                "assets": {
                    "high_value": [
                        {"id": "asset_1_id", "name": "asset_1_name"},
                        {"id": "asset_2_id", "name": "asset_2_name"},
                    ],
                    "low_value": [
                        {"id": "asset_3_id", "name": "asset_3_name"},
                        {"id": "asset_4_id", "name": "asset_4_name"},
                    ],
                },
            },
            {
                "type": "TYPEB",
                "assets": {
                    "high_value": [
                        {"id": "asset_1_id", "name": "asset_1_name"},
                        {"id": "asset_2_id", "name": "asset_2_name"},
                    ],
                    "low_value": [
                        {"id": "asset_3_id", "name": "asset_3_name"},
                        {"id": "asset_4_id", "name": "asset_4_name"},
                    ],
                },
            },
        ],
    },
    {
        "team": "X",
        "description": "A description of X",
        "types": [
            {
                "type": "TYPEC",
                "assets": {
                    "high_value": [
                        {"id": "asset_1_id", "name": "asset_1_name"},
                        {"id": "asset_2_id", "name": "asset_2_name"},
                    ],
                    "low_value": [
                        {"id": "asset_3_id", "name": "asset_3_name"},
                        {"id": "asset_4_id", "name": "asset_4_name"},
                    ],
                },
            }
        ],
    },
]

Where here, there are 2 dicts for team X, with different Types (TYPEA and TYPEB, then another with TYPEC), there will also be 2 dicts for Team Y, maybe 3 for team Z etc.

My desired outcome, is to have a list of dicts, where there is only 1 dict for Team X, that has Type A, B and C together.

*Have not listed desired outcome as makes for very long example. Happy to add.

Would look something like:

Code:
list_of_dicts = [
    {
        "team": "X",
        "description": "A description of X",
        "types": [
            {
                "type": "TYPEA",
                "assets": {...},
            {,
            {
                "type": "TYPEB",
                "assets": {...},
            {,
            {
                "type": "TYPEC",
                "assets": {...},
            {,
        ],
    },
]
<p>I have a list of dicts, where each dict refers to a team and its assets.</p>
<p>e.g.</p>
<pre class="lang-py prettyprint-override"><code>list_of_dicts = [
{
"team": "X",
"description": "A description of X",
"types": [
{
"type": "TYPEA",
"assets": {
"high_value": [
{"id": "asset_1_id", "name": "asset_1_name"},
{"id": "asset_2_id", "name": "asset_2_name"},
],
"low_value": [
{"id": "asset_3_id", "name": "asset_3_name"},
{"id": "asset_4_id", "name": "asset_4_name"},
],
},
},
{
"type": "TYPEB",
"assets": {
"high_value": [
{"id": "asset_1_id", "name": "asset_1_name"},
{"id": "asset_2_id", "name": "asset_2_name"},
],
"low_value": [
{"id": "asset_3_id", "name": "asset_3_name"},
{"id": "asset_4_id", "name": "asset_4_name"},
],
},
},
],
},
{
"team": "X",
"description": "A description of X",
"types": [
{
"type": "TYPEC",
"assets": {
"high_value": [
{"id": "asset_1_id", "name": "asset_1_name"},
{"id": "asset_2_id", "name": "asset_2_name"},
],
"low_value": [
{"id": "asset_3_id", "name": "asset_3_name"},
{"id": "asset_4_id", "name": "asset_4_name"},
],
},
}
],
},
]
</code></pre>
<p>Where here, there are 2 dicts for team X, with different Types (<code>TYPEA</code> and <code>TYPEB</code>, then another with <code>TYPEC</code>), there will also be 2 dicts for Team Y, maybe 3 for team Z etc.</p>
<p>My desired outcome, is to have a list of dicts, where there is only 1 dict for Team X, that has Type A, B and C together.</p>
<p>*Have not listed desired outcome as makes for very long example. Happy to add.</p>
<p>Would look something like:</p>
<pre class="lang-py prettyprint-override"><code>list_of_dicts = [
{
"team": "X",
"description": "A description of X",
"types": [
{
"type": "TYPEA",
"assets": {...},
{,
{
"type": "TYPEB",
"assets": {...},
{,
{
"type": "TYPEC",
"assets": {...},
{,
],
},
]
</code></pre>
 
Top