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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:45:25+00:00 2026-06-08T05:45:25+00:00

I have two dictionaries that I’d liked to compare their dict_values based on certain

  • 0

I have two dictionaries that I’d liked to compare their dict_values based on certain criteria, say the value at index 0 and 2 on their respective dict_values have to be the same to be considered equal.

This code works:

a = dict()
b = dict()
a[1,3] = 5, 6, 7
b[1,4] = 5, 9, 7
a_tuple = (list(a.values()))[0]
b_tuple = (list(b.values()))[0]
if a_tuple[0] == b_tuple[0] and a_tuple[2] == b_tuple[2]:
   print('Equal')

Is there a shorter/efficient way to do this ?.

Thanks in advance!

John

  • 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-08T05:45:28+00:00Added an answer on June 8, 2026 at 5:45 am

    I’m not sure if this is much better, but the following should work:

    import operator
    import sys
    
    py3k = sys.version_info[0] > 2
    
    a = dict()
    b = dict()
    a[1,3] = 5, 6, 7
    b[1,4] = 5, 9, 7
    
    if(py3k):
      a_tuple = next(iter(a.values()))
      b_tuple = next(iter(b.values()))
    else:
      a_tuple = a.values()[0]
      b_tuple = b.values()[0]
    
    cmp_key=operator.itemgetter(0,2)
    if cmp_key(a_tuple) == cmp_key(b_tuple):
      print('Equal')
    

    The comparison is a little more clean and I use next and iter to get the first tuple in the dictionary if using python 3.

    Of course, the better solution would be change the data structure so you’re not constantly dealing with dictionaries with only 1 item.

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

Sidebar

Related Questions

Here's my problem: Lets say I have two dictionaries, dict_a and dict_b. Each of
I have two tables (some so-called dictionaries, only about ~50 entries I presume) that
I have a main function that contains two dictionaries that I would like to
Possible Duplicate: python dict.add_by_value(dict_2) ? My input is two dictionaries that have string keys
I have two dictionaries with key-value pairs as follows: dict-1 ch:23, 100 ch:24, 95
I have two generic Dictionaries. Both have the same keys, but their values can
I have two separate Python List that have common key names in their respective
I have the following two functions which take two dictionaries and recursively add their
I have two dictionaries with a string key and different value types. private Dictionary<string,
I have two dictionaries of data and I created a function that acts as

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.