Introduction to Python print list without brackets

To print a list without brackets in python can be a very handy feature. It can be used in various useful projects. But before performing the above-mentioned task, we need to understand what is a list in python and how does it work.

What is a list in Python Programming?

In Python programming, a list is made by placing multiple elements or items inside square brackets ([ ]) along with commas. Lists are used to store multiple objects under a single variable. These objects can be of different types like string, integer, float, etc.

Example for the following statement:

os = ["Windows", "MacOS", "Linux", "ChromeOS", "FreeDOS"]
print(os)

The output of this piece of code will be:

["Windows", "MacOS", "Linux", "ChromeOS", "FreeDOS"]

Here we can see that the output of the list is displayed along with the brackets and commas. But if you want to remove these from the output, then follow these simple steps:-

How to print list without brackets Python?

Below are different ways by which we can print a list without brackets in python

1. Use Unpacking Method

This method uses the asterisk operator(*) to unpack the objects inside a list. You can print each object individually in the print function. You should remember one thing. All the print elements are separated by a blank space between them. Example for this statement:

os = ["Windows", "MacOS", "Linux", "ChromeOS", "FreeDOS"]
print(*os)

As a result, the Output will be:

Windows MacOS Linux ChromeOS FreeDOS

2. Unpacking Using The Sep Keyword

If you want to print a list with commas and without the square brackets, then we use the ‘sep’ keyword to separate the objects of the list. Example of this statement:

os = ["Windows", "MacOS", "Linux", "ChromeOS", "FreeDOS"]
print(*os, sep=', ')

As a result, the Output will be:

Windows, MacOS, Linux, ChromeOS, FreeDOS

3. Using The .join() Method In Python

Python has a built-in method known as join(). The function of this method is to take an object and convert it into a string. A separator is also used in this method. Preferably it is a “, “. Example of this statement:

os = ["Windows", "MacOS", "Linux", "ChromeOS", "FreeDOS"]
print(", ".join(os))

As a result, Output will be:

Windows, MacOS, Linux, ChromeOS, FreeDOS

You have to remember that if you want to join the integer values, then the join method wouldn’t work because it accepts only a string value. It will display the following error:

num = [1, 2, 3, 4]
print(", ".join(num))

As a result, the error in Output will be:

TypeError: sequence item 0: expected str instance, int found

But you can also use integers with the join method. You can accomplish this by using the map() function. An example is below:

num = [1, 2, 3, 4]
print(", ".join(map(str, num)))

Output:

1, 2, 3, 4

The map() function returns an object which is now converted into a string and then passes it to the join() method.

4. Using The str function In Python

So in this method, you can convert a list to a string using the str() function. Then you can remove the first and last characters from the string that are the square brackets. For example:

list = [1,2,3]
list_str = str(list)[1:-1] 
print(list_str)

Output:

1, 2, 3

Note: This method can also work with a list of integers or floats values.

5. Using For Loop

Last but not the least, this problem can also be solved using for loop. In this, we go through each value of a list and print that value. Example:

systems = ["Windows", "MacOS", "Linux", "ChromeOS", "FreeDOS"]

for system in systems:

print(system, end=", ")

print("\b\b", end="")

print(" ") 

Output:

Windows
MacOS
Linux
ChromeOS
FreeDOS

Steps:

  • Firstly, you declare the list.
  • Secondly, using the for loop you go through each value of a list and print that value with a comma and space.
  • Finally, we move the comma with space.

Also check other pyhton tutorial-

Like this article? Follow us on Facebook and LinkedIn.