9,434 views
34 votes
34 votes
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.

User Green Joffer
by
2.9k points

1 Answer

13 votes
13 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 Kevin Spaghetti
by
3.5k points