How to Print an Array with Commas in Python?

I will help you to understand how to print an array with commas in Python in this tutorial. As a data scientist working on a project for a US-based company, I recently faced an issue where I needed to display the elements of a NumPy array as a comma-separated string. After researching various methods, I discovered several effective ways to achieve this. I will explain these techniques, along with examples and screenshots of executed example code.

Convert NumPy Array to Comma-Separated String in Python

When working with NumPy arrays, you can use the np.array2string() function to convert the Python array to a comma-separated string. Here’s an example.

import numpy as np

cities = np.array(["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"])
city_string = np.array2string(cities, separator=", ")
print(city_string)

Output:

['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix']

Let me show you the screenshot of the example code which I executed.

Print an Array with Commas in Python

To remove the square brackets from the output, you can set the formatter parameter to None and use string slicing to remove the brackets as shown.

city_string = np.array2string(cities, separator=", ", formatter={'str_kind': lambda x: x})[1:-1]
print(city_string)

Output:

New York, Los Angeles, Chicago, Houston, Phoenix

Let me show you the screenshot of the example code which I executed.

How to Print an Array with Commas in Python

Check out How to Convert Python Dict to Array

Convert Python List to Comma-Separated String

If you have a Python list instead of a NumPy array, you can use the join() method to convert the list to a comma-separated string. Here’s an example:

states = ["California", "Texas", "Florida", "New York", "Illinois"]
state_string = ", ".join(states)
print(state_string)

Output:

California, Texas, Florida, New York, Illinois

Let me show you the screenshot of the example code which I executed.

Print an Array with Commas in Python use the join() method

The join() method takes an iterable (such as a list) and joins its elements into a string, using the specified separator.

Read How to Print Duplicate Elements in Array in Python

Print Python List with Commas Using print()

Another way to print a list with commas is by using the print() function with the sep parameter. This method works for both lists and NumPy arrays. Here’s an example:

presidents = ["George Washington", "John Adams", "Thomas Jefferson", "James Madison", "James Monroe"]
print(*presidents, sep=", ")

Output:

George Washington, John Adams, Thomas Jefferson, James Madison, James Monroe

The * before the list name unpacks the list, passing each element as a separate argument to the print() function. The sep the parameter specifies the separator to use between the elements, which in this case is a comma followed by a space.

Check out How to Update an Array in Python

Pandas DataFrame Column to Comma-Separated String in Python

When working with Pandas DataFrames, you may need to convert a column containing strings to a comma-separated list. You can achieve this using the str.cat() method.

import pandas as pd

data = {
    "Name": ["John Smith", "Emma Davis", "Michael Johnson", "Olivia Williams", "William Brown"],
    "Age": [28, 32, 45, 37, 51]
}
df = pd.DataFrame(data)

name_list = df['Name'].str.cat(sep=', ')
print(name_list)

Output:

John Smith, Emma Davis, Michael Johnson, Olivia Williams, William Brown

Read How to Find the Sum of an Array in Python

Conclusion

In this tutorial, I have explained how to print an array with commas in Python. We explored several effective ways to achieve this, like convert numPy array to comma separated string, where we used np.array2string() function to convert the Python array, converting list to comma separated String using the join() method is another method, print Python list with commas using print() is also covered in this article, we also discussed, pandas DataFrame column to comma separated string in Python.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.