Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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):
        self.id = 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)
                     .ToList();

Is there an easy way to do this in python?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading