Mason
Mason

Reputation: 19

Return list of string if substring is in list of strings

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

Upvotes: 0

Views: 61

Answers (2)

rafi_
rafi_

Reputation: 1

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

Here b is your search string

Upvotes: 0

GoodDeeds
GoodDeeds

Reputation: 8497

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']

Upvotes: 6

Related Questions