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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:58:39+00:00 2026-05-12T08:58:39+00:00

I have a Python list of strings, e.g. initialized as follows: l = [‘aardvark’,

  • 0

I have a Python list of strings, e.g. initialized as follows:

l = ['aardvark', 'cat', 'dog', 'fish', 'tiger', 'zebra']

I would like to test an input string against this list, and find the “closest string below it” and the “closest string above it”, alphabetically and case-insensitively (i.e. no phonetics, just a<b etc). If the input exists in the list, both the “below” and “above” should return the input.

Several examples:

Input  | Below    |  Above   
-------------------------------
bat    | aardvark | cat      
aaa    | None     | aardvark 
ferret | dog      | fish     
dog    | dog      | dog

What’s the neatest way to achieve this in Python? (currently I’m iterating over a sorted list using a for loop)

To further clarify: I’m interested in simple dictionary alphabetical comparison, not anything fancy like Levenshtein or phonetics.

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-12T08:58:40+00:00Added an answer on May 12, 2026 at 8:58 am

    This is exactly what the bisect module is for. It will be much faster than just iterating through large lists.

    import bisect
    
    def closest(haystack, needle):
        if len(haystack) == 0: return None, None
    
        index = bisect.bisect_left(haystack, needle)
        if index == 0:
            return None, haystack[0]
        if index == len(haystack):
            return haystack[index], None
        if haystack[index] == needle:
            return haystack[index], haystack[index]        
        return haystack[index-1], haystack[index]
    

    The above code assumes you’ve sanitized the input and list to be all upper or lower case. Also, I wrote this on my iPhone, so please do check for typos.

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

Sidebar

Related Questions

I have a list of strings in Python - elements . I would like
I have a (python) list of strings which refer to python source files and
I have a Python list variable that contains strings. Is there a function that
In Python I have four strings that include the formatting of a list: line1
In Python I have a list of strings, some of which may be the
So I have a list of 4 strings in Python and I want to
Hi I have list of strings in python [ 0, 1, 2] and I
Let's say I have a list in python with several strings in it. I
Provided that I have a Python list of strings of words, how do I
Very rusty with my Python. I have a list of Cost as strings. I'm

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.