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

  • Home
  • SEARCH
  • 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 7650399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:14:35+00:00 2026-05-31T11:14:35+00:00

Given the following dictionary and set: d = {1 : a, 2 : b,

  • 0

Given the following dictionary and set:

d = {1 : a, 2 : b, 3 : c, 4 : d, 5 : e }
s = set([1, 4])

I was wondering if it is possible to remove all dictionary entries that are not contained in the set (i.e. 2,3,5).
I am aware that i can achieve this by iterating over the dictionary and check each key but since i’m new to Python and came across many “shortcuts” so far I was wondering if there exists one for this particular problem.

  • 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-05-31T11:14:37+00:00Added an answer on May 31, 2026 at 11:14 am
    d = {1 : 'a', 2 : 'b', 3 : 'c', 4 : 'd', 5 : 'e' }
    s = set([1, 4])
    

    Since you should not modify a dictionary while itering over it, you have two possibilities to create a new dictionary.

    One is to create a new dictionary from the old one filtering values out:

    d2 = dict((k,v) for k,v in d.iteritems() if k in s)
    

    The second one is to extract the keys, intersect them with the s-set and use them to build a new dictionary:

    d2 = dict((k, d[k]) for k in set(d) & s)
    

    The third one is to remove the elements directly from d:

    for k in set(d) - s:
        del d[k]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following code (that doesn't work): while True: # Snip: print out current
Given a dictionary (or Counter) of tally data like the following: d={'dan':7, 'mike':2, 'john':3}
So, given then following code type MyClass () = let items = Dictionary<string,int>() do
I am getting the following error: The given key was not present in the
There's an existing function that ends in the following, where d is a dictionary:
I am trying to grab all the items in the dictionary based on given
Given following Ruby statements: (Read input and store each word in array removing spaces
// given following array: $data = array( 0=>array( data=>object1, col=>array( 0=>array( data=>object2, col=>array( 0=>array(
Given the following XML: <current> <login_name>jd</login_name> </current> <people> <person> <first>John</first> <last>Doe</last> <login_name>jd</login_name> </preson> <person>
Given the following: List<List<Option>> optionLists; what would be a quick way to determine the

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.