<p>What would be your preferred way to concatenate strings from a sequence such that between every two consecutive pairs a comma is added. That is, how do you map, for instance, <code>['a', 'b', 'c']</code> to <code>'a,b,c'</code>? (The cases <code>['s']</code> and <code>[]</code> should be mapped to <code>'s'</code> and <code>''</code>, respectively.)</p>

<p>I usually end up using something like <code>''.join(map(lambda x: x+',',l))[:-1]</code>, but also feeling somewhat unsatisfied.</p>

[{'answer_id': 44781, 'author': 'Mark Biek', 'author_id': 305, 'author_profile': 'https://Stackoverflow.com/users/305', 'pm_score': 11, 'selected': True, 'text': "<pre><code>my_list = ['a', 'b', 'c', 'd']\nmy_string = ','.join(my_list)\n</code></pre>\n\n<pre><code>'a,b,c,d'\n</code></pre>\n\n<p>This won't work if the list contains integers</p>\n\n<hr>\n\n<p>And if the list contains non-string types (such as integers, floats, bools, None) then do:</p>\n\n<pre><code>my_string = ','.join(map(str, my_list)) \n</code></pre>\n"}, {'answer_id': 44788, 'author': 'jmanning2k', 'author_id': 1480, 'author_profile': 'https://Stackoverflow.com/users/1480', 'pm_score': 6, 'selected': False, 'text': "<p>Why the <code>map</code>/<code>lambda</code> magic? Doesn't this work?</p>\n\n<pre><code>&gt;&gt;&gt; foo = ['a', 'b', 'c']\n&gt;&gt;&gt; print(','.join(foo))\na,b,c\n&gt;&gt;&gt; print(','.join([]))\n\n&gt;&gt;&gt; print(','.join(['a']))\na\n</code></pre>\n\n<p>In case if there are numbers in the list, you could use list comprehension:</p>\n\n<pre><code>&gt;&gt;&gt; ','.join([str(x) for x in foo])\n</code></pre>\n\n<p>or a generator expression:</p>\n\n<pre><code>&gt;&gt;&gt; ','.join(str(x) for x in foo)\n</code></pre>\n"}, {'answer_id': 44791, 'author': 'Douglas Leeder', 'author_id': 3978, 'author_profile': 'https://Stackoverflow.com/users/3978', 'pm_score': 4, 'selected': False, 'text': '<p>Don\'t you just want:</p>\n\n<pre><code>",".join(l)\n</code></pre>\n\n<p>Obviously it gets more complicated if you need to quote/escape commas etc in the values. In that case I would suggest looking at the csv module in the standard library:</p>\n\n<p><a href="https://docs.python.org/library/csv.html" rel="noreferrer">https://docs.python.org/library/csv.html</a></p>\n'}, {'answer_id': 44794, 'author': 'David Singer', 'author_id': 4618, 'author_profile': 'https://Stackoverflow.com/users/4618', 'pm_score': 1, 'selected': False, 'text': "<p>Unless I'm missing something, <code>','.join(foo)</code> should do what you're asking for.</p>\n\n<pre><code>&gt;&gt;&gt; ','.join([''])\n''\n&gt;&gt;&gt; ','.join(['s'])\n's'\n&gt;&gt;&gt; ','.join(['a','b','c'])\n'a,b,c'\n</code></pre>\n\n<p>(edit:  and as jmanning2k points out, </p>\n\n<pre><code>','.join([str(x) for x in foo])\n</code></pre>\n\n<p>is safer and quite Pythonic, though the resulting string will be difficult to parse if the elements can contain commas -- at that point, you need the full power of the <code>csv</code> module, as Douglas points out in his answer.)</p>\n"}, {'answer_id': 44878, 'author': 'Peter Hoffmann', 'author_id': 720, 'author_profile': 'https://Stackoverflow.com/users/720', 'pm_score': 3, 'selected': False, 'text': '<p>@jmanning2k using a list comprehension has the downside of creating a new temporary list. The better solution would be using itertools.imap which returns an iterator</p>\n\n<pre><code>from itertools import imap\nl = [1, "foo", 4 ,"bar"]\n",".join(imap(str, l))\n</code></pre>\n'}, {'answer_id': 46233, 'author': 'Aaron Maenpaa', 'author_id': 2603, 'author_profile': 'https://Stackoverflow.com/users/2603', 'pm_score': 4, 'selected': False, 'text': '<p>@Peter Hoffmann</p>\n\n<p>Using generator expressions has the benefit of also producing an iterator but saves importing itertools. Furthermore, list comprehensions are generally preferred to map, thus, I\'d expect generator expressions to be preferred to imap.</p>\n\n<pre><code>&gt;&gt;&gt; l = [1, "foo", 4 ,"bar"]\n&gt;&gt;&gt; ",".join(str(bit) for bit in l)\n\'1,foo,4,bar\' \n</code></pre>\n'}, {'answer_id': 65255, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<pre><code>l=[\'a\', 1, \'b\', 2]\n\nprint str(l)[1:-1]\n\nOutput: "\'a\', 1, \'b\', 2"\n</code></pre>\n'}, {'answer_id': 156851, 'author': 'jfs', 'author_id': 4279, 'author_profile': 'https://Stackoverflow.com/users/4279', 'pm_score': 4, 'selected': False, 'text': '<p>Here is a alternative solution in Python 3.0 which allows non-string list items:</p>\n\n<pre><code>&gt;&gt;&gt; alist = [\'a\', 1, (2, \'b\')]\n</code></pre>\n\n<ul>\n<li><p>a standard way</p>\n\n<pre><code>&gt;&gt;&gt; ", ".join(map(str, alist))\n"a, 1, (2, \'b\')"\n</code></pre></li>\n<li><p>the alternative solution</p>\n\n<pre><code>&gt;&gt;&gt; import io\n&gt;&gt;&gt; s = io.StringIO()\n&gt;&gt;&gt; print(*alist, file=s, sep=\', \', end=\'\')\n&gt;&gt;&gt; s.getvalue()\n"a, 1, (2, \'b\')"\n</code></pre></li>\n</ul>\n\n<p>NOTE: The space after comma is intentional.</p>\n'}, {'answer_id': 35319592, 'author': 'Ricky Sahu', 'author_id': 1484447, 'author_profile': 'https://Stackoverflow.com/users/1484447', 'pm_score': 5, 'selected': False, 'text': '<p><code>",".join(l)</code> will not work for all cases. I\'d suggest using the csv module with StringIO</p>\n\n<pre><code>import StringIO\nimport csv\n\nl = [\'list\',\'of\',\'["""crazy"quotes"and\\\'\',123,\'other things\']\n\nline = StringIO.StringIO()\nwriter = csv.writer(line)\nwriter.writerow(l)\ncsvcontent = line.getvalue()\n# \'list,of,"[""""""crazy""quotes""and\\\'",123,other things\\r\\n\'\n</code></pre>\n'}, {'answer_id': 43978261, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>Here is an example with list</p>\n\n<pre><code>&gt;&gt;&gt; myList = [[\'Apple\'],[\'Orange\']]\n&gt;&gt;&gt; myList = \',\'.join(map(str, [i[0] for i in myList])) \n&gt;&gt;&gt; print "Output:", myList\nOutput: Apple,Orange\n</code></pre>\n\n<p>More Accurate:-</p>\n\n<pre><code>&gt;&gt;&gt; myList = [[\'Apple\'],[\'Orange\']]\n&gt;&gt;&gt; myList = \',\'.join(map(str, [type(i) == list and i[0] for i in myList])) \n&gt;&gt;&gt; print "Output:", myList\nOutput: Apple,Orange\n</code></pre>\n\n<p>Example 2:-</p>\n\n<pre><code>myList = [\'Apple\',\'Orange\']\nmyList = \',\'.join(map(str, myList)) \nprint "Output:", myList\nOutput: Apple,Orange\n</code></pre>\n'}, {'answer_id': 44085343, 'author': 'Shameem', 'author_id': 6690588, 'author_profile': 'https://Stackoverflow.com/users/6690588', 'pm_score': 3, 'selected': False, 'text': '<pre><code>&gt;&gt;&gt; my_list = [\'A\', \'\', \'\', \'D\', \'E\',]\n&gt;&gt;&gt; ",".join([str(i) for i in my_list if i])\n\'A,D,E\'\n</code></pre>\n\n<p><code>my_list</code> may contain any type of variables. This avoid the result <code>\'A,,,D,E\'</code>.</p>\n'}, {'answer_id': 48137821, 'author': 'Roberto', 'author_id': 5548995, 'author_profile': 'https://Stackoverflow.com/users/5548995', 'pm_score': -1, 'selected': False, 'text': "<p>My two cents. I like simpler an one-line code in python:</p>\n\n<pre><code>&gt;&gt;&gt; from itertools import imap, ifilter\n&gt;&gt;&gt; l = ['a', '', 'b', 1, None]\n&gt;&gt;&gt; ','.join(imap(str, ifilter(lambda x: x, l)))\na,b,1\n&gt;&gt;&gt; m = ['a', '', None]\n&gt;&gt;&gt; ','.join(imap(str, ifilter(lambda x: x, m)))\n'a'\n</code></pre>\n\n<p>It's pythonic, works for strings, numbers, None and empty string. It's short and satisfies the requirements. If the list is not going to contain numbers, we can use this simpler variation:</p>\n\n<pre><code>&gt;&gt;&gt; ','.join(ifilter(lambda x: x, l))\n</code></pre>\n\n<p>Also this solution doesn't create a new list, but uses an iterator, like @Peter Hoffmann pointed (thanks).</p>\n"}, {'answer_id': 50912527, 'author': 'Ron Kalian', 'author_id': 7134286, 'author_profile': 'https://Stackoverflow.com/users/7134286', 'pm_score': 1, 'selected': False, 'text': "<p>I would say the <code>csv</code> library is the only sensible option here, as it was built to cope with all csv use cases such as commas in a string, etc.</p>\n\n<p>To output a list <code>l</code> to a .csv file:</p>\n\n<pre><code>import csv\nwith open('some.csv', 'w', newline='') as f:\n    writer = csv.writer(f)\n    writer.writerow(l)  # this will output l as a single row.  \n</code></pre>\n\n<p>It is also possible to use <code>writer.writerows(iterable)</code> to output multiple rows to csv.</p>\n\n<p>This example is compatible with Python 3, as the other answer here used <code>StringIO</code> which is Python 2.</p>\n"}, {'answer_id': 62599395, 'author': 'faiz-e', 'author_id': 10014426, 'author_profile': 'https://Stackoverflow.com/users/10014426', 'pm_score': 2, 'selected': False, 'text': "<p>If you want to do the shortcut way :) :</p>\n<pre><code>','.join([str(word) for word in wordList])\n</code></pre>\n<p>But if you want to show off with logic :) :</p>\n<pre><code>wordList = ['USD', 'EUR', 'JPY', 'NZD', 'CHF', 'CAD']\nstringText = ''\n\nfor word in wordList:\n    stringText += word + ','\n\nstringText = stringText[:-2]   # get rid of last comma\nprint(stringText)\n</code></pre>\n"}, {'answer_id': 70126183, 'author': 'Cristián Vargas Acevedo', 'author_id': 4450951, 'author_profile': 'https://Stackoverflow.com/users/4450951', 'pm_score': 0, 'selected': False, 'text': "<p>mmm also need for SQL is :</p>\n<pre><code>l = [&quot;foo&quot; , &quot;baar&quot; , 6]\nwhere_clause = &quot;..... IN (&quot;+(','.join([ f&quot;'{x}'&quot; for x in l]))+&quot;)&quot;\n&gt;&gt; &quot;..... IN ('foo','baar','6')&quot;\n</code></pre>\n<p>enjoit</p>\n"}]
['https://Stackoverflow.com/questions/44778', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4285/']