1

I am trying to write a function where it takes a list that contains both numbers and strings and returns only a list containing numbers. The code I have written is shown below, but it keeps throwing an error and I can’t understand what I have done wrong. Hope you can help.

lst = [99, 'no data', 95, 94, 'no data'] 

def foo(lst):
    return [x for x in lst if x.isdigit()] 

print(foo(lst))
norok2
  • 25,683
  • 4
  • 73
  • 99
  • 4
    `int` doesn't have the attribute `isdigit`, so you cannot call that to determine if it's a digit or not. Try something like: `[x for x in lst if isinstance(x, int)]` – Hampus Larsson May 25 '20 at 10:53
  • please mention if you want to separate only `int` and `string` or you are looking to separate `string` which is numeric .. because in given example some are integer and others are strings – ashish singh May 25 '20 at 10:53
  • Does this answer your question? [How to properly use python's isinstance() to check if a variable is a number?](https://stackoverflow.com/questions/11204789/how-to-properly-use-pythons-isinstance-to-check-if-a-variable-is-a-number) – norok2 May 25 '20 at 11:06
  • ooo I see. Thanks so much for your help everyone, I really appreciate it! – Neethu Mathew May 26 '20 at 09:33

6 Answers6

1
lst = [99, 'no data', 95, 94, 'no data']

def foo(lst):
    return [x for x in lst if isinstance(x, int)]

print(foo(lst))
kaleco
  • 49
  • 3
1
lst = [99, 'no data', 95, 94, 'no data'] 

def foo(lst):
    return [x for x in lst if isinstance(x,str)] 

print(foo(lst))
marksman123
  • 389
  • 2
  • 11
1
lst = [99, 'no data', 95, 94, 'no data'] 

def isDigit(n):
    return type(n) is int
def foo(lst):
    return [x for x in lst if isDigit(x)] 

print(foo(lst))
Tuan
  • 94
  • 1
  • 7
0
lst = [99, 'no data', 95, 94, 'no data'] 

def foo(lst):
    return [x for x in lst if type(x)==int] 

print(foo(lst))
  • Please don't post only code as an answer, but also provide an explanation what your code does and how it solves the problem of the questions. Answers with an explanation are usually of higher quality, more useful for future visitors, and more likely to attract upvotes. – Mark Rotteveel May 25 '20 at 11:42
0

The error is because the isdigit() only works on string

lst = [99, 'no data', 95, 94, 'no data']

def foo(lst):
    return [lst[x] for x in range(0,len(lst)) if str(lst[x]).isdigit() ]


print(foo(lst))

Output:

[99, 95, 94]
error404
  • 2,684
  • 2
  • 13
  • 21
0

The idea is to check if each of the element's type is int or not. As mentioned in the comments, isDigit is not available for elements of type int.

lst = [99, 'no data', 95, 94, 'no data']
list=[]
def foo(lst):
    return [x for x in lst if type(x)==int]

print(foo(lst))

output:

[99, 95, 94]
Pardhu
  • 5,935
  • 2
  • 11
  • 23
  • 1
    Please don't post only code as an answer, but also provide an explanation what your code does and how it solves the problem of the questions. Answers with an explanation are usually of higher quality, more useful for future visitors, and more likely to attract upvotes. – Mark Rotteveel May 25 '20 at 11:40