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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:17:52+00:00 2026-05-26T11:17:52+00:00

I have a data in form a dictionary.. NOw I take the input from

  • 0

I have a data in form a dictionary..
NOw I take the input from the user and it can be anything..
And I am trying to do the following.
If the key exists then cool.. fetch the value from the dictionary.
if not, then fetch the nearest (in numeric sense).
For example..if the input key is 200
and the keys are like :….

197,202,208...

Then probably 202 is the closest key to 200..
Now, from algorithm point of view. its straight forward.. but is there a pythonic way to do this?
Thanks

  • 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-26T11:17:53+00:00Added an answer on May 26, 2026 at 11:17 am

    here’s your function on one line:

    data.get(num, data[min(data.keys(), key=lambda k: abs(k-num))])
    

    edit: to not evaluate the min when the key is in the dict use:

    data[num] if num in data else data[min(data.keys(), key=lambda k: abs(k-num))]
    

    or if all values in data evaluate to True you can use:

    data.get(num) or data[min(data.keys(), key=lambda k: abs(k-num))]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data coming from the database in the form of a DataSet .
I have some data of the form Key ID Link 1 MASTER 123 2
I have the following code I use to insert form data into a single
I have the following form <form name=myForm id=myForm method=post enctype=multipart/form-data action=script.php> and this jQuery
I'm developing an ASP.NET form for data-entry. Users have to select a client from
I have a WPF / XAML form data-bound to a property in a dictionary,
I have a dictionary of form data that I want to modify using a
I have data in this form, Article ID Company A Company B Company C
i have a submit page in html . when i submit the form data,
I have an upload box... <form action=upload_file.php method=post enctype=multipart/form-data><BR> <label for=file>Filename:</label><BR> <input type=file name=file

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.