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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:45:43+00:00 2026-06-02T08:45:43+00:00

I would like to compare elements in a dictionary to one another, and remove

  • 0

I would like to compare elements in a dictionary to one another, and remove items according to some comparison criteria. And I’d love it to be efficient. I have a function that can do this, but it repeatedly copies the dictionay. Surely there is a superior way:

mydict = {1:5,2:7,3:9,4:9,7:7,8:0,111:43,110:77}

def partial_duplicate_destroyer(mydict,tolerance):
    for key1 in mydict.keys():
        mydict_copy = mydict.copy()
        for key2 in mydict_copy.keys():
            if key2 - tolerance < key1 < key2 + tolerance and not(key1 == key2):
                del(mydict[key1])
                break
    return mydict

print partial_duplicate_destroyer(mydict,2)
print partial_duplicate_destroyer(mydict,20)
print partial_duplicate_destroyer(mydict,200)

#correct output:
# {4: 9, 8: 0, 111: 43}
# {8: 0, 111: 43}
# {111: 43}
  • 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-02T08:45:44+00:00Added an answer on June 2, 2026 at 8:45 am

    This approach could be reduced down to:

    from itertools import combinations
    
    def partial_duplicate_destroyer(mydict, tolerance):
        #Modifies in-place. Returns only as a convenience. Copy if you don't want this behaviour.
        for key1, key2 in combinations(mydict, 2):
           if key1 in mydict and key2 - tolerance < key1 < key2 + tolerance:
             del mydict[key1]
        return mydict
    

    If we try this:

    >>> mydict = {1:5,2:7,3:9,4:9,7:7,8:0,111:43,110:77}
    >>> partial_duplicate_destroyer(mydict, 2)
    {4: 9, 8: 0, 111: 43}
    >>> partial_duplicate_destroyer(mydict, 20)
    {8: 0, 111: 43}
    >>> partial_duplicate_destroyer(mydict, 200)
    {111: 43}
    >>> mydict
    {111: 43}
    

    This uses itertools.combinations() to produce all possible combinations of keys (without repeating). This is much more efficient as you don’t bother working where you have the keys the same, and it’s done more efficiently in C rather than your side with Python.

    Note that here you are modifying mydict in place – that is at the end of this, mydict is {111: 43} – you need to copy the dict and work on it in the function, rather than directly on it, if you don’t want this behaviour. This is what is shown in the last line.

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

Sidebar

Related Questions

I'm looking to compare two varchars in SQL, one would be something like Cafe
I would like to compare lists of elements of a given type, to see
I would like to compare 4 character string using wildcards. For example: std::string wildcards[]=
I would like to compare or monitor a sql file in the Database of
In my Nant script I would like to compare a property value to a
I have two arrays that I would like to compare and ultimately wind up
string word1 = misisipi; string word2 = mississippi; I would like to 'compare' these
I would like to be able to compare two classes derived from the same
I would like to know how to compare 2 different strings through a function
I would like to make a query which needs to compare an property's property

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.