Home > Back-end >  How to get a whole number from a list?
How to get a whole number from a list?

Time:10-22

I have a list:

num = ['9', '9', '2', '3']

So, how do i get a number 9923, for example in a variable?

CodePudding user response:

num = ['9','9','2','3']

new_num = ''.join(num)

try:
    new_num = int(new_num)
except ValueError as e:
    print("Not a number")

CodePudding user response:

n = ''.join(num)
n = int(n)
print(n)

output will be 9923

CodePudding user response:

Using base-10 expansion.

num = ['9','9','2','3']

decimal_num = sum(int(c)*10**i for i, c in zip(range(len(num)-1, -1, -1), num))

or maybe in more readable way

decimal_num = sum(int(c)*10**(len(num)-i-1) for i, c in enumerate(num))
  • Related