Print List without Square Brackets in Python (3 Examples)
In this tutorial, you’ll learn how to print a list without brackets in the Python programming language.
Here you have an overview of this tutorial:
Let’s dive into Python code!
Create Sample List
Here we will create a sample Python list to use as an example in this tutorial. Check the code below.
my_list = ["cat", 1, 2, "dog", 3, 4.2, "apple", 5] print(my_list) # ['cat', 1, 2, 'dog', 3, 4.2, 'apple', 5]
As shown, we have created a list object named
my_list that contains strings, integers, and floats. Then, we printed
my_list to the console using the print() function, which printed the list with square brackets.
But is there any way to print
my_list without these square brackets? Take a look at different examples of how to do it!
Example 1: Print List Without Brackets Using ‘*’ Operator
In this example, we will use the
* operator and the print() function to print
my_list without brackets. To do so, run the following script.
print(*my_list, sep = ', ') # cat, 1, 2, dog, 3, 4.2, apple, 5
As you can see, we have used the unpacking operator
* to unpack the list and pass its elements to the print() function. The
sep argument is used to specify the separator ‘, ‘ between the elements in
Example 2: Print List Without Brackets Using join() Method & map() Functions
print(', '.join(map(str, my_list))) # cat, 1, 2, dog, 3, 4.2, apple, 5
The previous Python output shows how to use the join() method to concatenate the elements of
my_list as strings, separated by a comma and a space. Then the
map(str, my_list) converts each element of the list to a string before joining them.
Example 3: Print List Without Brackets Using for Loop
This last method uses a for loop and string concatenation to print
my_list without square brackets. To do so, we will first create an empty string named
output = '' for item in my_list: output += str(item) + ', ' output = output[:-2] print(output) # cat, 1, 2, dog, 3, 4.2, apple, 5
As shown, this method involves iterating over
my_list and concatenating each element to form a string, separated by a comma and a space. We keep these elements in
output. Then, we use
[:-2] to remove the last comma and space from the string and print the final result.
Video, Further Resources & Summary
Do you need more explanations on how to print a list without square brackets in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
The YouTube video will be added soon.
There are some other tutorials on Statistics Globe you might be interested in:
- Print List in Custom Format in Python
- Find Index of Shortest & Longest String in List in Python
- Add Keys to List of Dictionaries in Python
- Add String to Each Element in List in Python
- Learn Python
This post has shown how to print a list without brackets in Python. In case you have further questions, don’t hesitate to let me know in the comments.
This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get more information about her academic background and the other articles she has written for Statistics Globe.
Statistics Globe Newsletter