Creating a dictionary with multiple values per key from a list of dictionaries

norpa

I have the following list of dictionaries:

listofdics = [{'StrId': 11, 'ProjId': 1},{'StrId': 11,'ProjId': 2},
              {'StrId': 22, 'ProjId': 3},{'StrId': 22, 'ProjId': 4},
              {'StrId': 33, 'ProjId': 5},{'StrId': 33, 'ProjId': 6},
              {'StrId': 34, 'ProjId': 7}]

I need to get all ProjId values for StrId that are duplicate. So this is the output I'm looking for:

new_listofdics = [{11:[1,2]}, {22:[3,4]}, {33:[5,6]], {34:[7]}]

I wrote a function that creates a list of dictionaries with StrId values as keys, and a list with all ProjId that share the same key as values. Here it is:

def compare_projids(listofdics):
    proj_ids_dups = {} 

    for row in listofdics:       
        id_value = row['StrId']
        proj_id_value = row['ProjId']
        proj_ids_dups[id_value]=proj_id_value

        if row['StrId'] == id_value:
            sum_ids = []
            sum_ids.append(proj_id_value)  
        proj_ids_dups[id_value]=sum_ids
     return proj_ids_dups

This is the output I get now:

new_listofdics=  {33: [6], 34: [7], 11: [2], 22: [4]}

What I see is thatappend replaces each ProjId value with the last one iterated, instead of adding them at the end of the list.

How can I fix this?...

vishes_shell

It's unclear why you need to have such output new_listofdics = [{11:[1,2]}, {22:[3,4]}, {33:[5,6]], {34:[7]}], because it's better to have just dict object.

So program would look like this

>>> from collections import defaultdict
>>> listofdics = [{'StrId': 11, 'ProjId': 1},{'StrId': 11,'ProjId': 2},
              {'StrId': 22, 'ProjId': 3},{'StrId': 22, 'ProjId': 4},
              {'StrId': 33, 'ProjId': 5},{'StrId': 33, 'ProjId': 6},
              {'StrId': 34, 'ProjId': 7}]
>>> output = defaultdict(list)
>>> for item in listofdics:
...     output[item.get('StrId')].append(item.get('ProjId'))
>>> dict(output)
{11: [1, 2], 22: [3, 4], 33: [5, 6], 34: [7]}

It's much easier to go through that dict that your desired output.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

List of dictionaries with multiple values per key as Dataframe

list to dictionary conversion with multiple values per key?

How to compare multiple arrays from dictionary values, and map per array element the dictionary key into new array/list

Creating list with dictionary instead of multiple dictionaries in python

Loop from excel data into a dictionary with multiple values per key

Selecting single value from dictionary with multiple values per key

How do I compare multiple key values from a list of dictionaries?

make dictionaries from list with multiple values for each key

Creating Dataframe from dictionary that has multiple values assigned to each key

Creating a dictionary from a txt file with key year and values a list

Getting key from values present in a dictionary of dictionaries

Split a dictionary with n values per key into n dictionaries

Multiple values per key (array or dictionary)?

Handling a dictionary with multiple values per key

Plotting a dictionary with multiple values per key

Update value in Dictionary with multiple values per key

Dictionary with multiple values per key via for loop

Finding the lowest value per key in a dictionary with multiple values per key

Convert a column of list of dictionaries to a column list such that the values are derived from the key "name" under each dictionary in the list

Deleting a key from a dictionary that is in a list of dictionaries

Create a new dictionary with the key-value pair from values in a list of dictionaries based on matches from a separate list

How to modify the values of a dictionary from a list of dictionaries

Creating a dictionary given values from a list and dictionary

Python3: Dividing multiple values per key in one dictionary, by multiple values per key in another dictionary

from a dictionary of dictionaries, return a list of the inner dictionaries, updated with the key

Appending a list with a dictionary key that contains multiple values from a json response

Converting dictionaries to list sorted by values, with multiple values per item

Python 3: Creating list of multiple dictionaries have same keys but different values coming from multiple lists

change dictionary values to dictionaries from list of dictionaries in python