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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:57:09+00:00 2026-05-24T13:57:09+00:00

I am trying to build a function in python that yields values of two

  • 0

I am trying to build a function in python that yields values of two dictionaries IF a particular value from dict1 matches a particular value of dict2. My function looks like this:

def dict_matcher(dict1, dict2, item1_pos, item2_pos):
"""Uses a tuple value from dict1 to search for a matching tuple value in dict2. If a match is found, the other values from dict1 and dict2 are returned."""
for item1 in dict1:
    for item2 in dict2:
        if dict1[item1][item1_pos] == dict2[item2][item2_pos]:
            yield(dict1[item1][2], dict2[item2][6])

I am using dict_matcher like this:

matches = [myresults for myresults in dict_matcher(dict1, dict2 , 2, 6)]
print(matches)

When I print matches I get a list of correctly matching dict1 and dict2 values like this:

[('frog', 'frog'), ('spider', 'spider'), ('cricket', 'cricket'), ('hampster', 'hampster')]

How can I add variable arguments to this function so that, in addition to printing the matching values from each dictionary, I can also print the other values of each dictionary item in instances where dict1[item1][2] and dict2[item2][6] match? Can I use *args? Thanks for the help.

EDIT:
Ok, there seems to be some confusion as to what I am trying to do so let me try another example.

dict1 = {1: ('frog', 'green'), 2: ('spider', 'blue'), 3: ('cricket', 'red')}

dict2 = {a: ('frog', 12.34), b: ('ape', 22.33), c: ('lemur', 90.21)}

dict_matcher(dict1, dict2, 0, 0) would find matching values for value[0] from dict1 and value[0] from dict2. In this case, the only match is ‘frog’. My function above does this. What I am trying to do is extend the function to be able to print out other values from the dictionary items where dict1[value][0] == dict2[value][0] I want this to be specified in the function argument.

  • 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-24T13:57:09+00:00Added an answer on May 24, 2026 at 1:57 pm

    You said you’re calling it as

    matches = [myresults for myresults in dict_matcher(dict1, dict2 , 2, 6)]
    

    You should be calling it as

    matches = list(dict_matcher(dict1, dict2 , 2, 6))
    

    and it’s signature is

    def dict_matcher(dict1, dict2, item1_pos, item2_pos, *args):
    

    So 4 arguments passed, and 4 named arguments. So *args results in args = None.

    I’m not sure exactly what you want, but if you do

    yield dict1[item1][item1_pos], dict2[item2][item2_pos]
    

    You’ll get the same thing as you get from doing

    yield dict1[item1][2], dict2[item2][6]
    

    If you want to get the whole matching items, do

    yield dict1[item1], dict2[item2]
    

    If you want to get one item from each, but not the matching item, do

    def dict_matcher(dict1, dict2, item1_pos, item2_pos, other1_pos, other2_pos):
    

    and

    yield dict1[item1][other1_pos], dict2[item2][other2_pos]
    

    and

    matches = list(dict_matcher(dict1, dict2 , 2, 6, 3, 8)) 
    

    or whatever instead of 3 and 8.

    If you want to get several, but not all items, do

    def dict_matcher(dict1, dict2, item1_pos, item2_pos, other1_poss, other2_poss):
    

    and

    yield [dict1[item1][i] for i in other1_poss], [dict2[item2][i] for i in other2_poss]
    

    and

    matches = list(dict_matcher(dict1, dict2 , 2, 6, (2, 3), (6, 8))) 
    

    or whatever instead of [2, 3] and [3, 8].

    If this isn’t what you meant, let me know.

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

Sidebar

Related Questions

I am trying to build a function that extracts information from a database and
Using PyClips, I'm trying to build rules in Clips that dynamically retrieve data from
i'm trying to build a function that will tell me the range of a
I am trying to build a php function that would return an array with
I am trying to build Python from source and need to include the SSL
I am trying to build a function in C/C++ to sort an array and
I am trying to build an Eclipse application that would work with a linux/motif
I am trying to build PyGTK on CentOS for a non-standard Python (2.6, vs
I'm trying to embed Python into a MATLAB mex function on OS X. I've
I've been trying to make a dict subclass inheriting from UserDict.DictMixin that supports non-hashable

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.