Im trying figure out if there is a simple way to group a few objects by their variables?
I have tried to loop through the objects, creating seperate sets then again create new objects but I feel like my approaches are way too complicated, is there any common simple way?
class Dog:
def __init__(self, name, age):
self.name = name
self.age = age
dogs = [Dog('Buddy', 3), Dog('Max', 3), Dog('Charlie', 5),
Dog('Cooper', 1), Dog('Jack', 3),Dog('Rocky', 4), Dog('Bear', 1),
Dog('Duke', 3), Dog('Ace', 5)]
grouped_dogs = [(Dog('Buddy', 3), Dog('Max', 3), Dog('Duke', 3)),
(Dog('Charlie', 5)), (Dog('Cooper', 1), Dog('Bear', 1)),
(Dog('Rocky', 4))] # expected output
I wish it could be grupped by age like grouped_dogs in the end.