469,356 Members | 2,016 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

Combining digit in a list to make an integer

I have the following:

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

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

thanks,

Harlin Seritt

Jul 18 '05 #1
5 1754
Harlin Seritt wrote:
I have the following:

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

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

int(''.join(num1))

Jul 18 '05 #2
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

Jul 18 '05 #3

Harlin Seritt wrote:
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.

Jul 18 '05 #4
Harlin Seritt wrote:
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:
'<'.join(['1', '2', '3'])

'1<2<3'

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

Jul 18 '05 #5
On 1 Apr 2005 03:21:12 -0800, Harlin Seritt <ha**********@yahoo.com> wrote:
num1 = ['1', '4', '5']

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

num1 = ['1', '4', '5']
int(''.join(num1))

145

.. Facundo

Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Rune Strand | last post: by
5 posts views Thread by Bunyip Bluegum | last post: by
6 posts views Thread by Smythe32 | last post: by
8 posts views Thread by Candace | last post: by
33 posts views Thread by Prasad | last post: by
3 posts views Thread by PeyYang | last post: by
8 posts views Thread by Marc | last post: by
14 posts views Thread by thehobbit | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.