199k views
4 votes
he function below takes one parameter: a list of strings (string_list). Complete the function to return a new list containing only the strings from the original list that are less than 20 characters long.

User Mahin Khan
by
3.5k points

2 Answers

1 vote

Answer:

# the solution function is defined

# it takes a list as parameter

def solution(string_list):

# an empty new list is initialized

new_list = []

# a loop that loop through the splitted_list

# it checks if the length of each string is less than 20

# if it is less than 20

# it is attached to the new list

for each_string in string_list:

if(len(each_string) < 20):

new_list.append(each_string)

# the new list is printed

print(new_list)

# the function is called with a sample list

solution(["The", "player", "determined", "never", "compromised"])

Step-by-step explanation:

The program is written in Python and it is well commented.

User DaveCat
by
3.5k points
3 votes

Answer:

def select_short_strings(string_list):

new_list = []

for s in string_list:

if len(s) < 20:

new_list.append(s)

return new_list

lst = ["apple", "I am learning Python and it is fun!", "I love programming, it is easy", "orange"]

print(select_short_strings(lst))

Step-by-step explanation:

- Create a function called select_short_strings that takes one argument string_list

Inside the function:

- Initialize an empty list to hold the strings that are less than 20

- Inside the loop, check the strings inside string_list has a length that is smaller than 20. If found one, put it to the new_list.

- When the loop is done, return the new_list

- Create a list to check and call the function

User Thothal
by
3.5k points