I have a list in the following form:
full_list = [1, 2, 5, "string", False, True]
I want to return a list containing only the non-digit values, like this:
new_list = ["string", False, True]
My initial thought is to use the isdigit() method, but that only works for strings. I could cast each item in the list as a string, and then call isdigit(), but
new_list = [i for i in full_list if not str(i).isdigit()]
feels like a longwinded workaround. The solution posted here sort-of works, but will remove the boolean values from the final list. Is there a more elegant/simplistic way of accomplishing this?
Assuming
you want to discard integers (1, 2, 3) but not strings that represent integers ('1', '2', '3')
you want each element to be unique in the result (what your title suggests)
the order does not matter
You could use set
s. And use Number
as the class to check, plus check bool
as well because True
/False
are Number
instances as well.
from numbers import Number
full_list = [1, 2, 5, "string", False, True]
set(full_list) - set(
i in full_list if isinstance(i, Number) and not isinstance(i, bool))
Number
covers floats as well. Your example only has integers but you didn't mention explicitly that all numbers should be integers.