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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:33:21+00:00 2026-06-05T20:33:21+00:00

I am looking for a more efficient way to do comparisons between all elements

  • 0

I am looking for a more efficient way to do comparisons between all elements of a python dict.

Here is psuedocode of what I am doing:

for key1 in dict:
    for key2 in dict:
        if not key1 == key2:
            compare(key1,key2)

if the length of the dict is N, this is N^2 – N. Is there any way of not repeating the elements in the second loop? For lists, this would be:

N = len(list)
for i in range(1:(N-1)):
    for j in range((i+1):N):
        compare(list[i], list[j])

anyway to do this for the dict case?

  • 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-05T20:33:22+00:00Added an answer on June 5, 2026 at 8:33 pm

    Maybe something like

    >>> import itertools
    >>> 
    >>> d = {1:2, 2:3, 3:4}
    >>> 
    >>> for k0, k1 in itertools.combinations(d,2):
    ...     print 'compare', k0, k1
    ... 
    compare 1 2
    compare 1 3
    compare 2 3
    

    if you don’t care about whether you get (1,2) or (2,1). [Of course you could iterate over sorted(d) or some variant if you wanted a particular order, or compare both (k0, k1) and (k1, k0) if that mattered.]

    [BTW: don’t call your lists list or your dicts dict– that clobbers the builtins, and they’re handy to have around.]

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

Sidebar

Related Questions

Greetings all, I'm looking for a more efficient way to grab multiple users without
I've been looking at PPK's article here as, perhaps, a more efficient way of
I am looking for a more jquery/cleaner way to be able to select all
I'm looking for a more efficient way of completing this task. I need to
I am looking for a more efficient way to do this...(see below)... I have
I'm looking for a more efficient way to reprioritize items in a priority queue.
I'm looking for a more efficient way to generate some DOM changes and update
I'm still learning Perl, so there's probably a more efficient way of doing this.
I'm looking for a more efficient way to build this query (double subqueries make
I'm looking for a more efficient way to build a semicolon delimited list of

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.