Here was my solution to the problem:
word = 'cake'
cakeList = []
mixedList = ["cake", 12, "cakecar", "cAKe", "orange", "apple", "1cake", "cake1"]
for x in mixedList:
if word in str(x).lower():
cakeList.append(x)
print cakeList
With the output: ['cake', 'cakecar', 'cAKe', '1cake', 'cake1']
Each element in mixedList is searched for the string 'cake', and if this is True, the element is appended into cakeList.
I am still a n00b at python, so this might not be the most efficient method of solving this problem.
Pretty nice code for a noob! You can simplify it a little with list comprehension ...
word = 'cake'
mixedList = ["cake", 12, "cakecar", "cAKe", "orange", "apple", "1cake", "cake1"]
cakeList = [x for x in mixedList if word in str(x).lower()]
print cakeList # ['cake', 'cakecar', 'cAKe', '1cake', 'cake1']
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417