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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:00:21+00:00 2026-06-10T19:00:21+00:00

Possible Duplicate: Python dictionaries – find second character in a 2-character string which yields

  • 0

Possible Duplicate:
Python dictionaries – find second character in a 2-character string which yields minimum value

I would like to submit the first item of a tuple key and return the remaining item of that key which minimizes the tuple key value.

For example:

d = {('a','b'): 100,
     ('a','c'): 200,
     ('a','d'): 500}

If I were to pass in 'a', I would like to return 'b'.

  • 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-10T19:00:23+00:00Added an answer on June 10, 2026 at 7:00 pm

    Sorting is unnecessary here:

    >>> d ={('a','b'):100,('a','c'):200,('a','d'):500,('b','c'):1000,('b','e'):100}
    >>> def func(d, k0):
    ...     return min((k for k in d if k[0] == k0), key=d.get)[1]
    ... 
    >>> func(d, 'a')
    'b'
    >>> func(d, 'b')
    'e'
    

    This works by using a generator expression to give only the keys in the dictionary whose first element matches the input, and uses as the associated “weight” (unfortunately named the “key” here in this context) the associated value in the dictionary. Dictionaries have a .get method which returns the value given a specific key, so that’s the natural one to use.

    Note that in the case of ties, this returns an arbitrary key.

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

Sidebar

Related Questions

Possible Duplicate: python dict.add_by_value(dict_2) ? My input is two dictionaries that have string keys
Possible Duplicate: python .rstrip removes one additional character i have a string vtype defined
Possible Duplicate: Python: Get object by id Typically when you see the string representation
Possible Duplicate: python .rstrip removes one additional character What is the problem here? it
Possible Duplicate: Python Ternary Operator If Python would support the (x ? a :
Possible Duplicate: Python program to find fibonacci series. More Pythonic way. Hey, i was
Possible Duplicate: reverse a string in Python I tried searching the python documentation for
Possible Duplicate: Python, Printing multiple times, I'd like to know how to print a
Possible Duplicate: python random string generation with upper case letters and digits How do
Possible Duplicate: Python read a single character from the user I am looking to

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.