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

kepler

I have a list of dictionaries in Python:

items = [
    { 'color': 'blue', 'shape': 'square' },
    { 'color': 'green', 'shape': 'triangle' },
    { 'color': 'yellow', 'shape': 'circle' },
    { 'color': 'green', 'shape': 'diamond' },
    { 'color': 'blue', 'shape': 'oval' }
]

I need to go over this list so that I can select items based on the uniqueness of the color value to perform further actions on common items. So in this example the first iteration should produce the following list:

output_list = [
    { 'color': 'blue', 'shape': 'square' },
    { 'color': 'blue', 'shape': 'oval' }
]

The second iteration:

output_list = [
    { 'color': 'green', 'shape': 'triangle' },
    { 'color': 'green', 'shape': 'diamond' }
]

The third iteration:

output_list = [
    { 'color': 'yellow', 'shape': 'circle' }
]
Rashid 'Lee' Ibrahim

While the other two answers will indeed give you the correct results and are extremely clear to read. I think it's smart to point out those answers have an O(n^2) run times. For an O(n) efficient run time, you will need to cut the loops down to one.

output_dict = {}

for item in items:
    if item['color'] not in output_dict.keys():
        output_dict[item['color']] = []

    output_dict[item['color']].append(item)

return output_dict

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python, list of tuples split into dictionaries

Python: Uniqueness for list of lists

How to split a dictionary into a list of multiple dictionaries, all of size N in Python

Sorting a list of lists of dictionaries in python

Python - Filter list of dictionaries based on multiple keys

Python object as a list of lists of dictionaries

Python: Split a list into multiple lists based on a subset of elements

Flatten list of dictionaries with multiple key, value pairs

Deserialize json to list of KeyValue pairs

code multiple columns based on lists and dictionaries in Python

combining multiple lists into one list in python

Split list of dictionaries into list of lists with list length based on a key value pair inside the dictionary

Removing duplicate dictionaries in list of dicts based on value uniqueness for a given key

Uniqueness in a list of lists with lists

Python Split String With Multiple Dictionaries

Split a list of dictionaries into multiple lists of dictionaries in python

Python - if the total chars in a list exceeds X then split the list into multiple lists

Python - Split list to multiple lists with respect to a keyword

java Split one list into multiple lists by exclusion of elements

Split list of object in multiple lists based on sum of values and size

Split a list into multiple lists with each one having a set amount of items

Split List based on range into multiple list :PYTHON

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

Python: split a list into multiple lists based on a specific element

Split a list into pairs using python

Transform a list of lists into a list of numbers based on uniqueness

Python - Create list of dictionaries from multiple lists of values

Split pfd based off value and Merge dictionaries inside list in python

Split a list into list of lists based on modulus of index in Python

TOP Ranking

HotTag

Archive