122k views
1 vote
Define a function below, filter_only_strs, which takes a single argument of type list. Complete the function so that it returns a list that contains only the strings from the original list and no other data. It is acceptable to return an empty list if there are no strings in the original list.

1 Answer

5 votes

Answer:

In Python:

def filter_only_str(mylist):

for x in mylist:

if isinstance(x, str) == False:

mylist.remove(x)

return mylist

Step-by-step explanation:

The function is not given. So, I write from scratch.

In Python:

This defines the function

def filter_only_str(mylist):

This iterates through the list

for x in mylist:

This checks if list item is not string

if isinstance(x, str) == False:

If true that list item is not string, the item is removed from the list

mylist.remove(x)

Return list

return mylist

User Lamarr
by
5.2k points