How to Print a List Without Brackets in Python?

5/5 - (5 votes)

Problem Formulation

Given a list of elements. If you print the list to the shell using print([1, 2, 3]), the output is enclosed in square brackets like so: [1, 2, 3]. But you want the list without brackets like so: 1, 2, 3.

print([1, 2, 3])
# Output: [1, 2, 3]
# Desired: 1, 2, 3

How to print the list without enclosing brackets?

Method 1: Unpacking

The asterisk operator * is used to unpack an iterable into the argument list of a given function. You can unpack all list elements into the print() function to print each of them individually. Per default, all print arguments are separated by an empty space. For example, the expression print(*my_list) will print the elements in my_list, empty-space separated, without the enclosing square brackets!

my_list = [1, 2, 3]
# Output: 1 2 3

To master the basics of unpacking, feel free to check out this video on the asterisk operator:

🌍 Recommended Tutorial: How to Print a List Without Brackets and Commas in Python?

Method 2: Unpacking with Separator

To print a comma-separated list without enclosing square brackets, the most Pythonic way is to unpack all list values into the print() function and use the sep=', ' argument to separate the list elements with a comma and a space. Specifically, the expression print(*my_list, sep=', ') will print the list elements without brackets and with a comma between subsequent list elements.

my_list = [1, 2, 3]
print(*my_list, sep=', ')
# Output: 1, 2, 3

You can learn about the ins and outs of the built-in print() function in the following video:

Python Print Function [And Its SECRET Separator & End Arguments]

Method 3: Slicing String Representation

Slicing is a concise way to access a subsequence from an original sequence. You can use slicing on the string representation of a list to access all characters except the first and last ones—that are the square bracket characters. For example, the expression print(str([1, 2, 3])[1:-1]) prints the list as "1, 2, 3" without enclosing square brackets.

my_list = [1, 2, 3]
# Output: 1, 2, 3

Feel free to dive into slicing next to boost your coding skills:

The Ultimate Guide to Slicing in Python

Method 4: String Join With Generator Expression

You can print a list without brackets by combining the string.join() method on the separator string ', ' with a generator expression to convert each list element to a string using the str() built-in function. Specifially, the expression print(', '.join(str(x) for x in my_list)) prints my_list to the shell without enclosing brackets.

my_list = [1, 2, 3]
print(', '.join(str(x) for x in my_list))
# Output: 1, 2, 3

You can modify the separator string on which you join to customize the appearance of the list:

my_list = [1, 2, 3]
print(' '.join(str(x) for x in my_list))
# Output: 1 2 3

You can dive deeper into generators in the following video:

Understanding Generators In Python

Also, before I’ll let you go, allow me to propose a related tutorial on the Finxter blog that solves a similar but different problem: printing a list without quotes.

🌍 Recommended Tutorial: Print a List Without Quotes in Python – 5 Best Ways

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

Programmer Humor

❓ Question: How did the programmer die in the shower? ☠️

❗ Answer: They read the shampoo bottle instructions:
Lather. Rinse. Repeat.