How to Print a List Without Brackets in Python?


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]
print(*my_list)
# Output: 1 2 3

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

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:

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]
print(str(my_list)[1:-1])
# Output: 1, 2, 3

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

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
  • The string.join(iterable) method concatenates the elements in the given iterable.
  • The str(object) built-in function converts a given object to its string representation.
  • Generator expressions or list comprehensions are concise one-liner ways to create a new iterable based by reusing elements from another iterable.

You can dive deeper into generators in the following video:

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And 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?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become 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.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!



Source link

Latest articles

Related articles

Leave a reply

Please enter your comment!
Please enter your name here