Python join strings with comma
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, Show I usually end up using
something like
Georgy 10.8k7 gold badges62 silver badges68 bronze badges asked Sep 4, 2008 at 21:04
1
This won't work if the list contains integers And if the list contains non-string types (such as integers, floats, bools, None) then do:
answered Sep 4, 2008 at 21:06
Mark BiekMark Biek 143k54 gold badges155 silver badges200 bronze badges 4 Why the
In case if there are numbers in the list, you could use list comprehension:
or a generator expression:
Georgy 10.8k7 gold badges62 silver badges68 bronze badges answered Sep 4, 2008 at 21:08
jmanning2kjmanning2k 9,0694 gold badges30 silver badges23 bronze badges 0
answered Feb 10, 2016 at 15:43
Ricky SahuRicky Sahu 22.1k4 gold badges39 silver badges31 bronze badges 2 @Peter Hoffmann 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.
answered Sep 5, 2008 at 16:29
Aaron MaenpaaAaron Maenpaa 116k10 gold badges94 silver badges108 bronze badges 1 Here is a alternative solution in Python 3.0 which allows non-string list items:
NOTE: The space after comma is intentional. answered Oct 1, 2008 at 9:23
jfsjfs 382k179 gold badges944 silver badges1613 bronze badges Don't you just want:
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: https://docs.python.org/library/csv.html
twasbrillig 15.4k9 gold badges40 silver badges62 bronze badges answered Sep 4, 2008 at 21:09
Douglas LeederDouglas Leeder 51.1k9 gold badges91 silver badges136 bronze badges
answered May 20, 2017 at 11:31
ShameemShameem 2,45216 silver badges20 bronze badges
AlvaroAV 9,88510 gold badges56 silver badges88 bronze badges answered Sep 15, 2008 at 18:08 1 @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
answered Sep 4, 2008 at 21:57
Peter HoffmannPeter Hoffmann 53.8k15 gold badges74 silver badges58 bronze badges 2 Here is an example with list
More Accurate:-
Example 2:-
answered May 15, 2017 at 11:25 2
If you want to do the shortcut way :) :
But if you want to show off with logic :) :
answered Jun 26, 2020 at 16:53
faiz-efaiz-e 1823 silver badges21 bronze badges Unless I'm missing something,
(edit: and as jmanning2k points out,
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 answered Sep 4, 2008 at 21:10
David SingerDavid Singer 1,1621 gold badge10 silver badges11 bronze badges I would say the To output a list
It is also possible to use This example is compatible with Python 3, as the other answer here used answered Jun 18, 2018 at 15:08
Ron KalianRon Kalian 2,9742 gold badges14 silver badges22 bronze badges 1 mmm also need for SQL is :
enjoit answered Nov 26, 2021 at 14:44
My two cents. I like simpler an one-line code in python:
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:
Also this solution doesn't create a new list, but uses an iterator, like @Peter Hoffmann pointed (thanks). answered Jan 7, 2018 at 13:56
RobertoRoberto 3746 silver badges9 bronze badges 1 How do you join a list of strings with a comma?In Java, we can use String. join(",", list) to join a List String with commas.
How do you join a string with spaces in Python?The join() method is a string method and returns a string in which the elements of sequence have been joined by str separator. In this approach space is the separator.
How do you combine two strings in Python?Two strings can be concatenated in Python by simply using the '+' operator between them. More than two strings can be concatenated using '+' operator.
How do you join a string in a list in Python?You can concatenate a list of strings into a single string with the string method, join() . Call the join() method from 'String to insert' and pass [List of strings] . If you use an empty string '' , [List of strings] is simply concatenated, and if you use a comma , , it makes a comma-delimited string.
|