# What wrong with this code

#I’m trying to separate the numeric value in the list
x=[‘a’,1,‘x’,7,‘o’,4]
print(“The List :” + str(x))
for i in x :
if (i.isnumeric())== True:
print(i)

`isnumeric` works only on strings.
Either convert i to a string before calling isnumeric, or check for number types directly:

``if isinstance(i, int): ...``

If you’ve got different number types in the list, do `import numbers` and use `isinstance(i, numbers.Number)`.

@florian Thanku
i got the code

Blockquotex=[‘a’,1,‘x’,7,‘o’,4]
print(“The List :” + str(x))
for i in x :
i=str(i)
if (i.isnumeric())== True:
print(i)

but how can i use (if isinstance(i,int):

Hm. I suppose the code you’ve got works, right?

You could just do this:

``````for i in x:
if isinstance(i, int):
print(i)``````

List = [“Hello”, “World”, 234, “Python”, “Programming”,False,30,0.331]

for i in List:
if type(i) == int:
print("Found numeric value: ",i)
<
My Class mate came up with this solution but he can’t explain well my solution is better
or his can any one explain it.

Well, I’d say his solution is a little better because he doesn’t need to convert types. But he won’t find 0.331 with this either. Hello @Shamsi

It depends on what you are looking for.
In the loop that @florian is suggesting you got False as a result.
That is because False is equal to 0 (and True == 1).

In your friends loop you got the integers.
If you also wanted the 0.331 you could add:

if type(i) == int or type(i) == float:

to the loop.

It is just what you want to get as result that is the best solution.

These are all good solutions. Let us know what you try.

Thanks for help @florian hint help me lot thank you very much @florian and @ulfen69 your float variation is very helping me actually my first language was GWBasic which i learned in my school day’s after that i leave programming now i’m back and trying to learn new technologies
i leave programming world because of my course intro to programming which was used c language and in that i never understand functions.

1 Like