I am working with amazon boto and I have 2 lists. List 1 contains Instance objects. List 2 contains InstanceInfo objects. Both objects have a attribute called id. I need to get a list of Instance objects which id exists in the InstanceInfo list.
l1 = [Instance:i-04072534, Instance:i-06072536, Instance:i-08072538, Instance:i-0a07253a, Instance:i-e68fa1d6, Instance:i-e88fa1d8, Instance:i-ea8fa1da, Instance:i-ec8fa1dc]
l2 = [InstanceInfo:i-ec8fa1dc, InstanceInfo:i-ea8fa1da, InstanceInfo:i-e88fa1d8, InstanceInfo:i-e68fa1d6]
Wanted result:
l3 = [Instance:i-ec8fa1dc, Instance:i-ea8fa1da, Instance:i-e88fa1d8, Instance:i-e68fa1d6]
Right now I have it working through:
l3= []
for a in l1
for b in l2:
if a.id == b.id:
l3.append(a)
However, I have been told that I should replace this using set intersection. I have been looking at examples and it looks very straightforward. Yet I don’t see any examples working with objects.
I’ve been playing around for a bit and theoretically I can see it work, yet there might be some ‘advanced’ syntax that I may not know off. I am still learning python.
Here is something faster than Marcin’s answer (while being similar):
It is important to pre-calculate
ids_l1and to not writeif item.id in set(…), as the set would be reconstructed each time (as the full test expression is re-evaluated for each elementitem).Python sets give you fast element membership tests (
in). Such tests are much faster with sets than with lists (as the elements of a list must be read one by one, whereas the elements of a set are “hashed”).