You can do this with isinstance , but unlike another answer from user3100115 I would use blacklists that you don't need, not just a whitelist. Not sure what would be more appropriate for your special occasion, just adding this as an alternative. It also works without import.
lst = ['hello', 1, 2, 3, 4, 'goodbye', 'help'] filtered = [element for element in lst if not isinstance(element, str)] print(filtered)
Instead of understanding the list, you can also use the built-in filter . This returns the generator, so to print it directly, you must first convert it to a list. But if you are going to iterate over it (for example, using for -loop), do not convert it, and it will be faster and consume less memory due to the "lazy evaluation". You can achieve the same as in the example above if you replace the square brackets with parentheses.
lst = ['hello', 1, 2, 3, 4, 'goodbye', 'help'] filtered = list(filter(lambda element: not isinstance(element, str), lst)) print(filtered)
source share