How do I convert a negative int to individual digit in an array? Something like this...
x = -3987
arr = [-3,9,8,7]
I tried to do this but I got an error.
ValueError: invalid literal for int() with base 10: '-'
x = [int(i) for i in str(x)]
Here's how I will handle it. The answer was already provided by @Fuledbyramen.
x = -3987
#arr = [-3,9,8,7]
if x < 0:
arr = [int(i) for i in str(x)[1:]]
arr[0] *= -1
else:
arr = [int(i) for i in str(x)]
print (arr)
The output of this will be:
[-3,9,8,7]
If value of x was 3987
x = 3987
then the output will be:
[3,9,8,7]
arr = [int(i) for i in str(x)[1:]]