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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:44:30+00:00 2026-05-31T17:44:30+00:00

Searching a Python dictionary based on the value first, to get a key output

  • 0

Searching a Python dictionary based on the value first, to get a key output make sense to me. But what if we want to add another constraint to the search?

For instance, here I am searching a dictionary (multi-dimensional) for the lowest value, then returning the key of that lowest value:

minValue[id] = min(data[id].items(), key=lambda x: x[1])

Since this method only returns one key that matches that value, while there may be multiple, I want to add another constraint.

Is there an elegant way to add: return key that contains overall minimum value AND has the longest length of those matching ?

  • 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-31T17:44:31+00:00Added an answer on May 31, 2026 at 5:44 pm

    I think a specific example would be helpful to clarify what the dictionary looks like since python doesn’t directly provide a multi-dimensional dict.

    I assume that it looks something like this: data = {'a': 1, 'b': 2, 'b': 3} (note, this is not valid python!), so that what you when you do min(data[id].items(), key=lambda x: x[1]) you want it to return ('a', 1), and checking for the longest length matching would give, perhaps [(‘b’, 2), (‘b’, 3)].

    If that is what you mean, then the easiest way is to use a defaultdict with a set:

    >>> data = defaultdict(set)
    >>> data['a'].add(1)
    >>> data['b'].add(2)
    >>> data['b'].add(3)
    >>> min(data.items(), key=lambda x: min(x[1]))
    ('a': {1})
    >>> min(data.items(), key=lambda x: max(len(x[1])))
    ('b': {2, 3})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to store configurations (key/value) for a Python application and I am searching
I've been searching for a way in python to get only 4 digits on
I'm searching for a way to crop image on a web based python application
Well, probably a strange question, I know. But searching google for python and braces
Searching here I found that this question was already asked , but I think
Searching for possible ways to get cookie with httpOnly enabled, I cannot find any.
searching around trying to find a way to add index on suffix of a
Searching for a while now, but haven't found a suitable answer to my problem.
I'd been searching for a way to do cookie based authentication/sessions in Google App
I'd like to control iTunes through Objective-C ( I just can't get Python appscript

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.