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?
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?
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
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
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