Home > Blockchain >  Split a list of dictionaries into multiple lists based on uniqueness of one of the key/value pairs i
Split a list of dictionaries into multiple lists based on uniqueness of one of the key/value pairs i

Time:04-11

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' }
]

CodePudding user response:

You can do that using comprehensions,

First enumerate all of the possible colours in a set (no duplicates):
colors = {item['color'] for item in items}

Then you can create a list of items of the same colour:
outputs = [[item for item in items if item['color'] == color] for color in colors]

CodePudding user response:

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

CodePudding user response:

Another solution with O(n) efficient run time.

output_dict = {}

for item in items:
    output_dict.setdefault(item['color'], []).append(item)
    
return output_dict

CodePudding user response:

Another version, using itertools.groupby:

from itertools import groupby

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

for _, g in groupby(sorted(items, key=lambda k: k["color"]), lambda k: k["color"]):
    print(list(g))

Prints:

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