Hello, I was getting stuck on this challenge about using filter() function to filter out strings in a list. Basically, it’s asking me to “deduplicate the list and keep only the sublists that have the book title stored as a string.” Below is a screenshot of my code along with the provided challenge. Also, here is the link to the challenge: https://www.codecademy.com/courses/learn-intermediate-python-3/articles/built-in-higher-order-functions-in-python. Thanks for all your help in advance:
I recommend using the isinstance
function in the lambda function, because isinstance
returns True
if it is the given type, and returns False
if not.
lambda book: isinstance( book[1], str )
`
Thank you for the reply. I haven’t learned the isinstance
function yet but in the hints section, it says "Remember you can select an element in a list by its location, e.g. list[0]
, and you can check whether a value is a string, integer, or float with the type()
function. So, I was wondering how to use the type()
function to solve the problem.
Nevermind. I got it now :). below is my solution:
string_titles = filter(lambda book: type(book[1]) == str, books)
Again, thank you for your solution.
Indeed, writing the isinstance function in a lambda function would be one of the easiest solutions. If you haven’t learned it yet, you can find the function syntax in books or on any python educational website.