Select from a list of objects where a value occurs more than once

I have a list of the below custom object in python. I want to create a new list of objects where the ‘ledId’ more than once (like a list of duplicates based on an id)

The object

class CustomObject:
    def __init__(self, id, ledId): = id
        self.ledId = ledId

I usually use C# so I am wanting to do something like

var subList = myList.Where(obj => myList.Count(l => l.ledId == obj.ledId) > 1)

Is there an easy way to do this in python?

>Solution :

Rewriting you code in C# to Python would be something like this:

[a for a in some_list if len([b for b in some_list if b.ledId == a.ledId]) > 1]

Leave a Reply