Reputation:
def int(x):
a=[]
for i in x:
if type(i)==int:
a.append(i)
return a
I tried to make a list consist of integers from given list but output is []
.
What is wrong?
What ı want to do:
input=[1,2,3,"a"]
output=[1,2,3]
Upvotes: 0
Views: 37
Reputation: 117856
You can do this filtering using isinstance
in a list comprehension
>>> data = [1,2,3,"a"]
>>> [i for i in data if isinstance(i, int)]
[1, 2, 3]
As a side note do not shadow the name int
with your function name, nor the function name input
with your variable name.
Upvotes: 2