Python Dictionaries: Grouping Key, Value pairs based on a common key, value

user2899444

I have a list that contains dictionaries like this:

list1 = [{'name': 'bob', 'email': '[email protected]', 'address': '123 house lane', 
'student_id': 12345}, {'name': 'steve', 'email': '[email protected]',
'address': '456 house lane', 'student_id': 34567}, {'name': 'bob',
'email': '[email protected]', 'address': '789 house lane', 'student_id': 45678}]

Is there a way in python to group selected key, values pairs within a new dictionary based on 'name' value? For instance, something like this as an end result:

new_list = [
    {'name': 'bob', 
         {'emails': ['[email protected]', 
                    '[email protected]']}, 
         {'address': ['123 house lane', 
                    '789 house lane']},
    {'name': 'steve',
        {'email': ... },
        {'address': ... }}
      # let's assume the list1 has various entries at some point 
      # which may or may not have duplicate 'name' values
      # and new_list will hold the groupings
]
srekcahrai

The code below gives you nested dictionaries. Nested dictionaries give you faster processing to find the key while in list you have to create a loop.

list1 = [{'name': 'bob', 'email': '[email protected]', 'address': '123 house lane', 
'student_id': 12345}, {'name': 'steve', 'email': '[email protected]',
'address': '456 house lane', 'student_id': 34567}, {'name': 'bob',
'email': '[email protected]', 'address': '789 house lane', 'student_id': 45678}]

dict1 = {}
for content in list1:
    if content['name'] in [name for name in dict1]:
        dict1[content['name']] = {'emails': dict1[content['name']]['emails'] + [content['address']], 'addresses': dict1[content['name']]['addresses'] + [content['email']]}
    else:
        dict1[content['name']] = {'emails': [content['email']], 'addresses': [content['address']]}
print dict1

Output of the code is

{'steve': {'emails': ['[email protected]'], 'addresses': ['456 house lane']}, 'bob': {'emails': ['[email protected]', '789 house lane'], 'addresses': ['123 house lane', '[email protected]']}}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Remove key-value pair based on grouping from dictionaries in python

counting common key-value pairs in a list of dictionaries

How to find common key value pairs from within nested dictionaries

python filter list of dictionaries based on key value

python filter list of dictionaries based on key value

Split a list of dictionaries into multiple lists based on uniqueness of one of the key/value pairs in Python

extracting key-value pairs from a list of dictionaries in Python

Combine several dictionaries based on a common key value into one dictionary

Compare dictionaries and delete key:value pairs

Flatten list of dictionaries with multiple key, value pairs

Creating a list of dictionaries with numerous key value pairs

Returning key-value pairs in a list of dictionaries

Grouping mysql results by multiple key value pairs

Python 2.7 : Finding common elements based on one key value from 2 list of dictionaries

Zip key value pairs in python

Parse Key Value Pairs in Python

Get dictionaries in a list where the key:value pairs are in another list of dictionaries

Sum values in a list of lists of dictionaries using common key-value pairs

Combining JSON by common key-value pairs

Compare the keys of two dictionaries and create a dictionary with key, value pairs that does not match the key in the other dictionary - Python

Combine two ansible dictionaries by common value in key

Most efficient way to extract multiple key/value pairs from a list of dictionaries of dictionaries in Python?

How to unfold a python dictionary of lists based on key-value "pairs"?

How to categorize list of dictionaries based on the value of a key in python efficiently?

Adding key and value to dictionary in python based on other dictionaries

Create key, value pairs based on the querydata

Sort array of dictionaries based on key and value

clustering a list of dictionaries according to specific key/value pairs

Comparing two dictionaries and checking how many (key, value) pairs are equal

TOP Ranking

HotTag

Archive