If I define a function that yields a list of numbers and I want to join those numbers to form a new number what should I do?
For example: if f(6) == [2, 3, 5]
, I want 235
as result.
If I define a function that yields a list of numbers and I want to join those numbers to form a new number what should I do?
For example: if f(6) == [2, 3, 5]
, I want 235
as result.
Use str.join
with for
loop
l = [1, 2, 3]
In [75]: int(''.join(str(i) for i in l))
Out[75]: 235
Or use simple math:-
In [77]: s = 0
In [78]: for x in l:
....: s = s*10 + x
....:
In [79]: s
Out[79]: 235
Assuming the number you want to to represent is in base10, you can do:
sum([x*10**(len(l)-i-1) for i,x in enumerate(l)])