user3715701 user3715701 - 7 months ago 14
Python Question

Python how to let the list that word with certain alphabet append into another new list

You are given the following code. Complete the code to fill up listStrFound with all words in listStrWords with the letter 'i'. The words in listStrFound should not be repeated.

listStrWords = ['My name is Bob',
'This is a cup',
'I love coding']
listStrFound = []


For example

listStrFound = ['is','This','I','coding']

Answer

You need to iterate through your listStrWords list using for loops.

listStrWords = ['My name is Bob', 
                'This is a cup', 
                'I love coding']

listStrFound = []
for string in listStrWords: # Iterating through the strings in listStrWords
    for word in list(set(string.split(' '))): # Iterating through each word
        if ("i" in word or "I" in word) and (word not in listStrFound):
            listStrFound.append(word)

print(listStrFound)