Ask Question
24 October, 23:48

The function below takes a single argument: data_list, a list containing a mix of strings and numbers. The function tries to use the Filter pattern to filter the list in order to return a new list which contains only strings longer than five characters. The current implementation breaks when it encounters integers in the list. Fix it to return a properly filtered new list.

+2
Answers (1)
  1. 25 October, 01:14
    0
    def filter_strings (data_list):

    string_list = []

    for s in data_list:

    if type (s) = = str and len (s) > 5:

    string_list. append (s)

    return string_list

    Explanation:

    Create a function called filter_strings that takes data_list as a parameter

    Initialize a new list to hold the strings that are longer than 5 characters

    Initialize a for loop that iterates through the data_list

    Check the elements if they are string - use type function, and if their length is greater than 5 - use len function. If an element satisfies the both conditions, put it to the string_list

    When the loop is done, return the string_list
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “The function below takes a single argument: data_list, a list containing a mix of strings and numbers. The function tries to use the Filter ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers