-1

I have 2 lists of objects:

For example:

class Object1:
    def __init__(self, id):
         self.id = id

Object1List = []
Object1List.append(Object1("id", 1))
Object1List.append(Object1("id", 2))
Object1List.append(Object1("id", 3))

class Object2:
    def __init__(self, id):
        self.id = id

Object2List = []
Object2List.append(Object2("id", 3))
Object2List.append(Object2("id", 4))
Object2List.append(Object2("id", 5))
Object2List.append(Object2("id", 6))

How do I remove any objects from Object1List with the same id as one in Object2List?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
WhatTheWhat
  • 197
  • 3
  • 13

2 Answers2

4

I've added __repr__ methods to the classes such that printing is more meaningful (you don't have to do that):

class Object1:
    def __init__(self, id):
         self.id = id
    def __repr__(self):
        return f"Object1(id={self.id})"

class Object2:
    def __init__(self, id):
        self.id = id
    def __repr__(self):
        return f"Object2(id={self.id})"

Your lists are:

Object1List = [Object1(id=n) for n in range(1, 4)]
Object2List = [Object2(id=n) for n in range(3, 7)]
[Object1(id=1), Object1(id=2), Object1(id=3)]
[Object2(id=3), Object2(id=4), Object2(id=5), Object2(id=6)]

To get the expected output you could first collect the ids of Object2List in a set, and then filter out the corresponding Object1List objects with a list comprehension (set-lookup is very efficient):

Object2ListIDs = {obj.id for obj in Object2List}
Object1ListNew = [obj for obj in Object1List if obj.id not in Object2ListIDs]

Result:

[Object1(id=1), Object1(id=2)]
Timus
  • 10,974
  • 5
  • 14
  • 28
-1

Create a function with like:

def remove_replicates(list1, list2):
    temp = list1
    for obj1 in list1:
        for obj2 in list2:
            if obj1.id == obj2.id:
                temp.remove(obj1)
                continue
    
    return temp
  • 3
    Be careful when [removing items from a list while iterating over it](https://stackoverflow.com/q/6260089/2745495). – Gino Mempin Aug 11 '22 at 15:21
  • I'm just finding that out, if list 1 only has 1 item in, the code above errors. Is there any way to guard against that other than an if statement checking `len` – WhatTheWhat Aug 11 '22 at 16:18
  • 1
    @WhatTheWhat See https://stackoverflow.com/q/1207406/2745495 – Gino Mempin Aug 11 '22 at 16:42
  • presumable this only works if the objects are the same, what if they are different? – WhatTheWhat Aug 11 '22 at 18:35
  • @ginomempin Yep you're right. I just updated the code. (Not the most efficient way though) – BarisSayit Aug 11 '22 at 19:30
  • 1
    `temp = list1` does not make a copy of the list. It just gives it a new name, but it's the same list. See [How do I clone a list so that it doesn't change unexpectedly after assignment?](https://stackoverflow.com/q/2612802/2745495). See the link I already shared above on how to correctly remove while iterating. – Gino Mempin Aug 11 '22 at 23:56