0

I'm trying to make a sort of list lookup that does not need to be exact to return an output. I want to be able to enter as many letters as I want, then have the program return a word that would match.

words=["apple","banana","orange"]

If the user inputted "ng", it would return orange. But if the user typed "an", banana and orange would return. "a" would return all the items on this list. I've been trying to google this for forever but I have clearly not been asking the right questions. Any help would be appreciated

3
  • Does this answer your question? Index of substring in a python list of strings Commented May 17, 2020 at 3:25
  • 1
    What does "Ctr + F" have to do with anything? Commented May 17, 2020 at 3:28
  • I just said "Ctr + F" because that was the easiest way to explain it i guess Commented May 17, 2020 at 3:32

2 Answers 2

6

You can use:

words=["apple","banana","orange"]
query = "an" # string to search for
selected = [w for w in words if query in w]

In the above example, selected is:

['banana', 'orange']
Sign up to request clarification or add additional context in comments.

Comments

0

a = ['what', 'who', 'wholesome'] b = 'w' for i in a: if b in i: print(i)

Here b is your search string

1 Comment

Hey Abdullah, Welcome to StackOverflow. Please explain your answers by writing a few statements and code snippet, so that it would be easy to understand for others as well. Always use coding format when inserting code statements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.