what if the list contains values like integer numbers in list like
list1 = ['a', 'b', 'c', 'd',5,4,433,"fdff",44,2323]
contains values like 5,4,433, so how to filter them using list comprehensive in python?
Python List Comprehension
what if the list contains values like integer numbers in list like
list1 = ['a', 'b', 'c', 'd',5,4,433,"fdff",44,2323]
contains values like 5,4,433, so how to filter them using list comprehensive in python?
Python List Comprehension
You can do something like [i for i in list1 if type(i) == int] to select out the integers.
You can simply do:
list1=['a', 'b', 'c', 'd',5,4,433,"fdff",44,2323]
list2=[a for a in list1 if isinstance(a, int)]
You could also try filter with isinstance:
print(list(filter(lambda x: isinstance(x, int), list_1)))
This will do.
[i for i in list1 if type(i) is int]
Or
[i for i in list1 if str(i).isnumeric()]