-5

I need to get all values that are numbers from:

lst = [7, 18, 3, 'a', True, (2,3)]

So I need to get 7,8 and 3. How can I get that?

I tried using function isnumeric and isdigit. It returns error ->

AttributeError: 'int' object has no attribute 'isnumeric'
matszwecja
  • 6,357
  • 2
  • 10
  • 17
  • You tried doing something, please share the code you tried with, then we can help you figure out the problem. – Cow Nov 10 '22 at 08:37
  • 2
    `[x for x in lst if str(x).isnumeric()]` – Celius Stingher Nov 10 '22 at 08:39
  • How is it supposed to work for floats, e.g. `1.4`? – matszwecja Nov 10 '22 at 08:41
  • 1
    @CeliusStingher probably because question in not entirely clear and there's been a lot of similar questions answered already. I don't know what indentation has to do with anything. – alex Nov 10 '22 at 08:41
  • @alex Exactly my thought. If OP would have spent just a few mins looking around on SO using the search function he/she could probably solve it quicker than asking this question. Also no code.. – Cow Nov 10 '22 at 08:41

2 Answers2

1

You can try using type()

lst = [7, 18, 3, 'a', True, (2,3)]
new_lst = [i for i in lst if type(i) in [int, float]]
KillerRebooted
  • 470
  • 1
  • 12
-1

Use list comprehension

Here is a quick example

lst = [7, 18, 3, 'a', True, (2,3)]
[x for x in lst if isinstance(x, int)]

==> [7, 18, 3, True]

"True" gets in the way here cause

int.__subclasses__()
[<type 'bool'>]

But this is another question...

ML-1994
  • 46
  • 3
  • 2
    This doesn't answer the question. OP specifically gave the result they wanted and yours does not comply to that. – Cow Nov 10 '22 at 08:47
  • "I need to get all values that are numbers", by default in Python booleans are numbers... What do you want me to do ? Change the language standards? – ML-1994 Nov 10 '22 at 08:50
  • *So I need to get 7,8 and 3.* Maybe you should check the other answer which clearly works as intended. – Cow Nov 10 '22 at 09:02