Obtain a set of unique values from a dictionary of lists

Viewed 7349

I have a list of dictionaries in which the dictionaries also contain a list.

I want to generate a set of the values of the respective nested lists so that I end up with a set of all of the unique items (in this case, hobbies).

I feel a set is perfect for this since it will automatically drop any duplicates, leaving me with a set of all of the unique hobbies.

people = [{'name': 'John', 'age': 47, 'hobbies': ['Python', 'cooking', 'reading']},
          {'name': 'Mary', 'age': 16, 'hobbies': ['horses', 'cooking', 'art']},
          {'name': 'Bob', 'age': 14, 'hobbies': ['Python', 'piano', 'cooking']},
          {'name': 'Sally', 'age': 11, 'hobbies': ['biking', 'cooking']},
          {'name': 'Mark', 'age': 54, 'hobbies': ['hiking', 'camping', 'Python', 'chess']},
          {'name': 'Alisa', 'age': 52, 'hobbies': ['camping', 'reading']},
          {'name': 'Megan', 'age': 21, 'hobbies': ['lizards', 'reading']},
          {'name': 'Amanda', 'age': 19, 'hobbies': ['turtles']},
          ]

unique_hobbies = (item for item in people['hobbies'] for hobby in people['hobbies'].items())

print(unique_hobbies)

This generates an error:

TypeError: list indices must be integers or slices, not str

My comprehension is wrong, but I am not sure where. I want to iterate through each dictionary, then iterate through each nested list and update the items into the set, which will drop all duplicates, leaving me with a set of all of the unique hobbies.

2 Answers
Related