I work with amazon boto and I have 2 lists. List 1 contains instance objects. List 2 contains InstanceInfo objects. Both objects have an attribute named id. I need to get a list of instance objects that 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]
Required Result:
l3 = [Instance:i-ec8fa1dc, Instance:i-ea8fa1da, Instance:i-e88fa1d8, Instance:i-e68fa1d6]
Now I work through:
l3= [] for a in l1 for b in l2: if a.id == b.id: l3.append(a)
However, I was told that I should replace this using set intersection. I looked at examples, and it looks very simple. However, I do not see examples of working with objects.
I played a little, and theoretically I see how it works, but there may be some kind of βadvancedβ syntax that I may not know. I am still learning python.
source share