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 8420059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:47:14+00:00 2026-06-10T02:47:14+00:00

Here is what is probably a simple question, but I wasn’t able to find

  • 0

Here is what is probably a simple question, but I wasn’t able to find a straightforward answer for on my own.

Given two lists, one with only a list of ids, the other with all data, including some ids that we don’t care about:

all_data = [['abc', 123], ['cde', 234], ['fgh', 345]]

ids = ['abc', 'fgh']

what is the best way to get the following output, note that it keeps only those that have the same ids:
new_data = [['abc', 123], ['fgh', 345]]

My current code does something like:

for x in all_data:
    for y in ids:
         if x[0] == y:
              new_data.append(x)

What woud you do differently? Is there a built-in function that takes care of this that I missed somewhere?

(I say “something like” because it’s actually a very long sequence involving sets and all that which is why there is not “pythonic” one-liner to share.)

UPDATE:
Well you guys are fun.

How about I make it a little harder. What if instead of “all_data” I have a a dictionary all_data_dict that has several list entries of the same format as “all_data”? Following the rules, I’ll make sure to accept the answer to the original question, but if you all want to keep up with the fun, let’s see what we get!

  • 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-10T02:47:15+00:00Added an answer on June 10, 2026 at 2:47 am

    being that many have used dicts or LC I thought I should show filter

    >>> all_data = [['abc', 123], ['cde', 234], ['fgh', 345]]    
    >>> ids = set(['abc', 'fgh'])
    >>> values = filter(lambda value: value[0] in ids, all_data)
    >>> values
    [['abc', 123], ['fgh', 345]]
    >>> 
    

    as for the second part.

    >>> all_data_dict = {'abc':all_data, 'cde':all_data, 'fgh':all_data}
    >>> ids = set(['abc', 'fgh'])
    >>> dict(filter(lambda value: value[0] in ids, all_data_dict.items()))
    {'abc': [['abc', 123], ['cde', 234], ['fgh', 345]], 'fgh': [['abc', 123], ['cde', 234], ['fgh', 345]]}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm probably missing something simple here, but I can't find the answer elsewhere. I
this is probably too simple of a question, but here I go. I have
This is probably a simple one to answer, but I'm stuck, so here goes.
This a pretty simple question (I assume). Probably a repost, but I couldn't find
Newbie here with a frustrating, but probably simple question. I am trying to write
Simple question, but I'll bet that asking on here will probably be more straight
Probably something simple, but I'm pulling my hair out: Site in question here: dev.sauceboxcreative.com/karenpool/
Well this is probably a stupid question with a simple answer but when using
probably a simple question here but here we go: I'm trying to publish a
the answer for this question is probably simple but currently I'm stumped and any

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.