I'm very new to coding and am taking an online course at the moment. Using the CodeHS sandbox to make programs and having a bit of a hard time with this one. I am trying to make my program look through this list of names and return which ones have the letter "E" / "e" in them. I also want it to keep count of how many names are being found and eventually return that count. Any help appreciated, thanks in advance. Here is my code so far:
middle_names = ["Michael", "Abdallah", "Parvati", "Sanskriti", "Piper", "Samuel", "Lee", "Meg", "Michael", "Mohamed", "Sofia", "Ahmed Hani", "Josh", "Lawrence", "Mireya", "Mingyue", "Bradley Theodore", "McKenna", "Ali"]
def search():
if "e" in middle_names:
print middle_names
search()
Loop approaches
You need to cycle through each item in your list. This is commonly achieved via a
forloop:A couple of ways you can improve such an algorithm is to account for lower or upper case letters, e.g. via
str.casefold, and usereturnoryieldstatements to have the function give an output. Here's an example using a generator:Comprehensions
One Pythonic and efficient approach is to use a list comprehension:
Or, if you want a lazy approach, you can use a generator comprehension: