Print a list without the brackets and commas in Python

In this tutorial, we will learn how to print a list in Python without the brackets and commas in many different ways. For example, we can use for loop, the join() function, etc. Follow the below for more information.

What is a list in Python?

In Python, a list is a collection of ordered and changeable items. Lists are written with square brackets and commas and can contain any data type, including other lists.

Here’s an example of a list in Python:

# Create a list
myList = [1, 5, 6, 8]

# View a list with commas and bracket
print(myList)

Output

[1, 5, 6, 8]

We can see that the list’s output is presented beside the brackets and commas. If you want to remove the brackets and commas from the output of a list in Python, follow these simple steps

How to print a list without the brackets and commas?

Using the operator (*)

To print a list without the commas and brackets, this method utilizes the asterisk operator (*) to unpack the items within the list. The objects are then printed individually within the print function, separated by a blank space. It’s important to note that a blank space separates the print elements. See the code example below:

# Create a list
lstFruit = ["Apple", "Banana", "Orange", "Mango"]

# View a list with commas and bracket
print(*lstFruit)

Output

Apple Banana Orange Mango

Using the .join() Method 

In this example, we will use the .join() method to print a list without the commas and brackets. Python includes a built-in method called .join() that converts an object into a string and uses a separator. Check out the code below:

# Create a list
lstFruit = ["Apple", "Banana", "Orange", "Mango"]

# View a list with commas and bracket
print(" ".join(lstFruit))

Output

Apple Banana Orange Mango

It’s important to remember that the .join() method will only work for string values, not integers. You will encounter the error if you try to use it with integers. So, we can use the map() function to solve this problem.

# Create a list
lstNumber = [1, 3, 5, 6]

# View a list with commas and bracket
print(" ".join(map(str, lstNumber)))

Output

1 3 5 6

Using For loop

To print a list in Python without the square brackets and commas, we can use a for loop and print each element of the list separately. Here’s an example:

# Create a list
lstFruit = ["Apple", "Banana", "Orange", "Mango"]

# View a list with commas and bracket
for i in lstFruit:
    print(i, end=" ")

Output

Apple Banana Orange Mango

Summary

This tutorial demonstrated how to print a list without the commas and brackets in Python in many different ways. But it would be best if you used the operator(*) because it is easy to do and convenient. If you have any questions or would like further clarification, please leave a comment below. 

Have a great day!

Leave a Reply

Your email address will not be published. Required fields are marked *