-2

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.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

2 Answers2

1

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
Vishnu Upadhyay
  • 5,043
  • 1
  • 13
  • 24
0

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)])
  • @SatvikMashkaria You mean the basis? like with hexadecimals? This should work for any length of l. –  Nov 18 '14 at 12:03