list = ['5', '12', '4', '3', '5', '14', '16', '-2', '4', 'test']
I've a list containing 10 items. How to convert this into a list of integers? I'm getting errors because of 'test' in the list.
list = ['5', '12', '4', '3', '5', '14', '16', '-2', '4', 'test']
I've a list containing 10 items. How to convert this into a list of integers? I'm getting errors because of 'test' in the list.
Don't call a list literally list. (It shadows the builtin list function and may give you weird bugs later!)
Assuming you just want to disregard non-numeric values, you can do this in a comprehension by using a filter at the end:
>>> [int(x) for x in [
... '5', '12', '4', '3', '5', '14', '16', '-2', '4', 'test'
... ] if x.lstrip('-').isdigit()]
[5, 12, 4, 3, 5, 14, 16, -2, 4]
str.isnumeric won't pass a negative signstr.lstrip to remove the -, check .isnumeric, and convert to int if it is.str.isdigit in place of .isnumeric.list)t = ['5', '12', '4', '3', '5', '14', '16', '-2', '4', 'test']
t = [int(v) if v.lstrip('-').isnumeric() else v for v in t]
print(t)
# output
[5, 12, 4, 3, 5, 14, 16, -2, 4, 'test']
t = ['5', '12', '4', '3', '5', '14', '16', '-2', '4', 'test']
t = [int(v) for v in t if v.lstrip('-').isnumeric()]
print(t)
# output
[5, 12, 4, 3, 5, 14, 16, -2, 4]
t = [['5', '12', '4', '3', '5', '14', '16', '-2', '4', 'test'] for _ in range(3)]
t = [[int(v) if v.lstrip('-').isnumeric() else v for v in x] for x in t]
print(t)
# output
[[5, 12, 4, 3, 5, 14, 16, -2, 4, 'test'],
[5, 12, 4, 3, 5, 14, 16, -2, 4, 'test'],
[5, 12, 4, 3, 5, 14, 16, -2, 4, 'test']]
supposing you want to forget wrong values, what about
list = ['5', '12', '4', '3', '5', '14', '16', '-2', '4', 'test']
list2 = []
for _ in list:
try:
list2.append(int(_))
except:
pass
print(list2)
execution :
>>> list = ['5', '12', '4', '3', '5', '14', '16', '-2', '4', 'test']
>>> list2 = []
>>> for _ in list:
... try:
... list2.append(int(_))
... except:
... pass
...
>>> print(list2)
[5, 12, 4, 3, 5, 14, 16, -2, 4]
>>>
except: is rarely a good idea.supposing you want to forget wrong values, of course if the goal is different the implementation must be too ^^except:. At the absolute minimum except Exception:, but the name of the appropriate error is right in what you've written.append() (but int(_) is ok)?supposing you want to forget wrong values so even that case must be silently hidden.You are getting error because alphabets cannot be converted to integers. However integers maybe converted to strings.
You may solve you issue by using 'try' and 'except'
I will give you how it can be done
for i in range(len(list)):
try:
list[i] = int(list[i])
except:
print(i,'cannot be converted to string')
The try and except statements work just like if and else statements. If there is no error in the code then 'try' statement will work. Whenever there is an error, except statement will get into action.
You can imagine it as:
if (no_error): #try statement
do_these_things
elif (any_error): #except statement
do_these_instead
Use the following code:
list = ['5', '12', '4', '3', '5', '14', '16', '-2', '4', 'test']
print([int(i) for i in list if i.lstrip('+-').isdigit()])
Which will give the following output:
[5, 12, 4, 3, 5, 14, 16, -2, 4]
'-2'.isdigit() is False.