I'm new to Python Programming, this is my code;
myList = " Bob said, breakthrough study reveals that human and octopus brains have common features"
for word in myList.split():
if word[0] == "b":
print(word)
Can I simplify this code above to a list comprehension in Python?
Can't find the solution.
CodePudding user response:
Your existing approach seems OK to me but not sure if you want something like this or not with list comprehension. Note: your mylist
name has a typo.
mylist = " Bob said, breakthrough study reveals that human and octopus brains have common features"
word = [word for word in mylist.split() if word[0] == "b"]
print(*word)
CodePudding user response:
You can do it the following way:
my_list = " Bob said, breakthrough study reveals that human and octopus brains have common features"
[print(i) for i in my_list.split() if i.startswith("b")]