How to use join with lists with integers

Suppose that we have a list such:

our_list = [1, 4, 5, 8]

Using ”, ”.join(our_list) will not work because join expects strings and not integers.
TypeError: sequence item 0: expected string, int found

Solution:

", ".join([str(elem) for elem in our_list])

or even easier (and more pythonic) :P

", ".join(map(str, our_list))

 
python/joinintlist.txt · Last modified: 2011/11/25 10:45 (external edit)
Email address
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki