create a string of values separated by commas from values from a list in python

To create a string of comma-separated values from a list in Python, you can use the join() method of a string. The join() method is called on a delimiter string (in this case, a comma), and passed a list of strings that you want to join together.

Here is an example code snippet:

main.py
values = ['apple', 'banana', 'orange', 'pear']
separator = ', '
csv_string = separator.join(values)
print(csv_string)
118 chars
5 lines

In this example, we start with a list of values, and define a separator string as ', '. We then call the join() method on the separator, passing in the list of values. The result is a string containing each value from the list, separated by the delimiter.

The output of the code will be:

main.py
apple, banana, orange, pear
28 chars
2 lines

gistlibby LogSnag