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