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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:00:46+00:00 2026-06-09T12:00:46+00:00

I have a dictionary d1 and a list l1 . The dictionary keys are

  • 0

I have a dictionary d1 and a list l1.

The dictionary keys are strings, and the values are Objects I have defined myself. If it helps, I can describe the Object in more detail but for now, the objects have a list attribute names, and some of the elements of name may or may not appear in l1.

What I wanted to do was to throw away any element of the dictionary d1, in which the name attribute of the object in said element does not contain any of the elements that appear in l1.

As a trivial example:

l1 = ['cat', 'dog', 'mouse', 'horse', 'elephant', 
      'zebra', 'lion', 'snake', 'fly']

d1 = {'1':['dog', 'mouse', 'horse','orange', 'lemon'],
      '2':['apple', 'pear','cat', 'mouse', 'horse'], 
      '3':['kiwi', 'lime','cat', 'dog', 'mouse'], 
      '4':['carrot','potato','cat', 'dog', 'horse'], 
      '5':['chair', 'table', 'knife']}

so the resulting dictionary will be more or less the same but the elements of each list will be the key-value pairs from 1 to 4 excluding the fruit and vegetables, and will not contain a 5th key-value par as none of the furniture values appear in l1.

To do this I used a nested list/dictionary comprehension which looked like this:

d2 = {k: [a for a in l1 if a in d1[k]] for k in d1.keys()}
print(d2)

>>>>{'1': ['dog', 'mouse', 'horse'], 
     '3': ['cat', 'dog', 'mouse'], 
     '2': ['cat', 'mouse', 'horse'], 
     '5': [], 
     '4': ['cat', 'dog', 'horse']}

d2 = {k: v for k,v in d2.iteritems() if len(v)>0}
print(d2)

>>>>{'1': ['dog', 'mouse', 'horse'], 
     '3': ['cat', 'dog', 'mouse'], 
     '2': ['cat', 'mouse', 'horse'],  
     '4': ['cat', 'dog', 'horse'],}

This seems to work, but for big dictionaries, 7000+ items, it takes around 20 seconds to work through. In and of itself, not horrible, but I need to do this inside a loop that will iterate 10,000 times, so currently it’s not feasible. Any suggestions on how to do this quickly?

  • 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-09T12:00:47+00:00Added an answer on June 9, 2026 at 12:00 pm

    You are effectively computing the set intersection of each list occuring in the dictionary values with the list l1. Using lists for set intersections is rather inefficient because of the linear searches involved. You should turn l1 into a set and use set.intersection() or set membership tests instead (depending on whether it is acceptable that the result is a set again).

    The full code could look like this:

    l1 = set(l1)
    d2 = {k: [s for s in v if s in l1] for k, v in d1.iteritems()}
    d2 = {k: v for k, v in d2.iteritems() if v}
    

    Instead of the two dictionary comprehensions, it might also be preferable to use a single for loop here:

    l1 = set(l1)
    d2 = {}
    for k, v in d1.iteritems():
        v = [s for s in v if s in l1]
        if v:
            d2[k] = v
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dictionary whose keys are strings and values are numpy arrays, e.g.:
I have a dictionary where keys are strings, and values are integers. stats =
I would like to have a dictionary object contains string keys and values, which
I have a dictionary with ints for keys, and strings for values. I need
I have a Dictionary<string, List<Order>> and I want to have the list of keys
I have a function, that returns the next higher value of a Dictionary-Keys-List compared
I'm trying to loop over distinct values over a dictionary list: So I have
Can we have something like - Dictionary<string, string> dict = new Dictionary<string, string>(); List<string>
I have a List of Dictionaries that have keys of type string and values
I have a dictionary and want to get the list of all dictionary values

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.