User Tools

Site Tools


python:joinintlist

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

python:joinintlist [2013/03/16 17:41] (current)
Line 1: Line 1:
 +==== How to use join with lists with integers ====
  
 +
 +Suppose that we have a list such:
 +
 +<code python>
 +our_list = [1, 4, 5, 8]
 +</​code>​
 +
 +Using //", "​.join(our_list)//​ will not work because join expects strings and not integers. \\ 
 +**TypeError:​ sequence item 0: expected string, int found**
 +
 +Solution:
 +
 +<code python>
 +", "​.join([str(elem) for elem in our_list])
 +</​code>​
 +
 +or even easier (and more pythonic) :P
 +
 +<code python>
 +", "​.join(map(str,​ our_list))
 +</​code>​
python/joinintlist.txt ยท Last modified: 2013/03/16 17:41 (external edit)