santafee
santafee

Reputation: 11

How do i search a word that contain specific characters from input command?

so, i have assingment from my course, it requires me to find a word (or more) from a list that contain a specific character from input.

lets say i have this list

word = ["eat", "drink", "yoga", "swim"]

and when i given input A, it should return me

["eat", "yoga"]

Upvotes: 0

Views: 35

Answers (2)

kuro
kuro

Reputation: 3226

You can use list comprehension.

ch = input("enter character")
output = [w for w in word if ch.lower() in w]

You may want to add some checks on the input (e.g. input is a single character or not)

Upvotes: 3

Sunil Jaitade
Sunil Jaitade

Reputation: 56

try this

list = ["eat", "drink", "yoga", "swim"]
reslst = []
alpa = input("enter character") #convert into lowercase
ch = alpa.lower()
for i in list:
#check if character is in string
    if ch in i:
        reslst.append(i)
print(reslst)

Upvotes: 1

Related Questions