Finding names in given list of names

PHOTO EMBED

Mon Oct 24 2022 10:18:38 GMT+0000 (Coordinated Universal Time)

Saved by @L0uJ1rky45M #python

# list_of_names = [...]

# Define your own function for program to look at the first letter of that name and decide whther to write name into a list.
def find_names_starting_with(letter, names):
    result = [] # Create a variable called result and give it an empty list
    for name in names: # Use for loop to repeat following code for every names in given list
        if name[0] == letter: # Change the letter into desired first letter
            result.append(name) # Add name with desired first letter to the end of list

    return result # Return statements sends data from the function back to the main program.

# Define your own function for program to filter the names in the list based on the number of letters in the name.
def find_names_of_length(length, names): 
    result = [] # Create a variable called result and give it an empty list
    for name in names:
        if len(name) == length: # Function len() returns the length of any data type that’s a sequence. When you use it on a list, it will return the number of items in the list, and when you use it on a string, it returns the number of characters.
            result.append(name) # Add name with desired number of letters to the end of list

    return result 

# Insert your desired first letter in example shown below
# names_e = find_names_starting_with("E", list_of_names)
names_e = find_names_starting_with("E", list_of_names)
# Insert desired number of letters in the name in example shown below
# names_e_and_6 = find_names_of_length(6, names_e)
names_e_and_6 = find_names_of_length(6, names_e)

# Finally, print the two lists.
print(names_e)
print(names_e_and_6)
content_copyCOPY

https://thepythoncodingbook.com/3-defining-functions-in-python/