Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7737539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:01:38+00:00 2026-06-01T08:01:38+00:00

I am working with amazon boto and I have 2 lists. List 1 contains

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:01:40+00:00Added an answer on June 1, 2026 at 8:01 am

    Here is something faster than Marcin’s answer (while being similar):

    ids_l1 = set(x.id for x in l1)  # All ids in list 1
    intersection = [item for item in l2 if item.id in ids_l1]  # Only those elements of l2 with an id in l1
    

    It is important to pre-calculate ids_l1 and to not write if item.id in set(…), as the set would be reconstructed each time (as the full test expression is re-evaluated for each element item).

    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”).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get Heroku working with European buckets on amazon s3 webserivces using
I am working on an interface with the amazon product advertising API. I have
i am working on amazon s3 bucket. And i need to find a size
I'm working with Amazon's Product Advertising API and need some help on a few
I'm working on a small project to get myself acquainted with the Amazon web
I have a bunch of videos stored on my Amazon S3 storage. I'm working
Does anyone have any tips on working with Amazon S3 service and Flash? I
I'm working with the Amazon API. I need to convert local time (EDT) to
I'm trying to get a simple example with amazon MWS working. I downloaded the
I am working with Amazon In-app-purchases for Android on flash using native extentions. So,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.