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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:59:13+00:00 2026-06-13T08:59:13+00:00

Python dict key delete, if key pattern match with other dict key. e.g. a={‘a.b.c.test’:1,

  • 0

Python dict key delete, if key pattern match with other dict key.

e.g.

a={'a.b.c.test':1,  'b.x.d.pqr':2,  'c.e.f.dummy':3,  'd.x.y.temp':4}

b={'a.b.c':1,  'b.p.q':20}

result

a={'b.x.d.pqr':2,'c.e.f.dummy':3,'d.x.y.temp':4}`  
  • 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-13T08:59:14+00:00Added an answer on June 13, 2026 at 8:59 am

    If “pattern match with other dict key” means “starts with any key in the other dict”, the most direct way to write that would be like this:

    a = {k:v for (k, v) in a.items() if any(k.startswith(k2) for k2 in b)}
    

    If that’s hard to follow at first glance, it’s basically the equivalent of this:

    def matches(key1, d2):
        for key2 in d2:
            if key1.startswith(key2):
                return True
        return False
    
    c = {}
    for key in a:
      if not matches(key, b):
        c[key] = a[key]
    a = c
    

    This is going to be slower than necessary. If a has N keys, and b has M keys, the time taken is O(NM). While you can checked “does key k exist in dict b” in constant time, there’s no way to check “does any key starting with k exist in dict b” without iterating over the whole dict. So, if b is potentially large, you probably want to search sorted(b.keys()) and write a binary search, which will get the time down to O(N log M). But if this isn’t a bottleneck, you may be better off sticking with the simple version, just because it’s simple.

    Note that I’m generating a new a with the matches filtered out, rather than deleting the matches. This is almost always a better solution than deleting in-place, for multiple reasons:
    * It’s much easier to reason about. Treating objects as immutable and doing pure operations on them means you don’t need to think about how states change over time. For example, the naive way to delete in place would run into the problem that you’re changing the dictionary while iterating over it, which will raise an exception. Issues like that never come up without mutable operations.
    * It’s easier to read, and (once you get the hang of it) even to write.
    * It’s almost always faster. (One reason is that it takes a lot more memory allocations and deallocations to repeatedly modify a dictionary than to build one with a comprehension.)

    The one tradeoff is memory usage. The delete-in-place implementation has to make a copy of all of the keys; the built-a-new-dict implementation has to have both the filtered dict and the original dict in memory. If you’re keeping 99% of the values, and the values are much larger than the keys, this could hurt you. (On the other hand, if you’re keeping 10% of the values, and the values are about the same size as the keys, you’ll actually save space.) That’s why it’s “almost always” a better solution, rather than “always”.

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

Sidebar

Related Questions

New to python... I have the following class Key, that extends dict: class Key(
How can I obtain a list of key-value tuples from a dict in Python?
I've got a python dict with where each key corresponds to a heading, and
I have python dict object with key as datetime.date object and values as tuple
Subclassing a Python dict works as expected: >>> class DictSub(dict): ... def __init__(self): ...
I know that Python dict s will leak when items are removed (because the
Is there a single line method to check whether a Python 2d dict has
I'd like to work with a dict in python, but limit the number of
Is there a method like putAll() for dict in python? I did not find
I might be confused between hashmap in Java, and map / dict in Python.

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.