-1

In Python, I want to convert list into int.

so if i have input like this:

a=[1,2,3,4]

i want this output:

1234

so How it is possible?

patelnisheet
  • 124
  • 1
  • 3
  • 12

3 Answers3

3

You can use the join function in conjunction with a generator, like this:

a=[1,2,3,4]

int(''.join(str(i) for i in a))

Output:

1234
Ashish Acharya
  • 3,349
  • 1
  • 16
  • 25
0

With recursion:

a=[1,2,3,4]
def f(l):
    if not l: return 0
    return l[-1] + f(l[:-1]) * 10
print(f(a))

This outputs:

1234
blhsing
  • 91,368
  • 6
  • 71
  • 106
0

You can use generator comprehension in the following way: result = int(''.join((str(i) for i in a)))

This turns every item of the list to a string, joins the list together and then turns the whole thing back to an integer

john
  • 11