Combining digit in a list to make an integer

H

Harlin Seritt

I have the following:

num1 = ['1', '4', '5']

How can I combine the elements in num1 to produce an integer 145?

thanks,

Harlin Seritt
 
H

Harlin Seritt

If anyone has time, would you mind explaining the code that Dan Bishop
was so kind as to point out to me:

int(''.join(num1))

This worked perfectly for me, however, I'm not sure that I understand
it very well.

Thanks,

Harlin Seritt
 
J

Joel

Harlin said:
If anyone has time, would you mind explaining the code that Dan Bishop
was so kind as to point out to me:

int(''.join(num1))

This worked perfectly for me, however, I'm not sure that I understand
it very well.

Thanks,

Harlin Seritt

''.join(list of strings) is a python idiom for fast string
concatenation. ''.join(num1) would give "145". The function int() is
then used to convert the resulting string into an integer.
 
D

Dan Bishop

Harlin said:
If anyone has time, would you mind explaining the code that Dan Bishop
was so kind as to point out to me:

int(''.join(num1))

This worked perfectly for me, however, I'm not sure that I understand
it very well.

join(...)
S.join(sequence) -> string

Return a string which is the concatenation of the strings in the
sequence. The separator between elements is S.

For example:
'1<2<3'

If you don't want a separator, simply let S be the empty string ('').
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top