How to Print a Set Without Brackets in Python?

Problem Formulation

Given a set of elements. If you print the list to the shell using print({1, 2, 3}), the output is enclosed in curly brackets (braces) like so: {1, 2, 3}. But you want the set without brackets like so: 1, 2, 3.

print({1, 2, 3})
# Output: {1, 2, 3}
# Desired: 1, 2, 3

How to print the set 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 set 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_set) will print the elements in my_set, empty-space separated, without the enclosing square brackets!

my_set = {1, 2, 3}
# 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 set without enclosing curly brackets, the most Pythonic way is to unpack all set values into the print() function and use the sep=', ' argument to separate the set elements with a comma and a space. Specifically, the expression print(*my_set, sep=', ') will print the set elements without brackets and with a comma between subsequent set elements.

my_set = {1, 2, 3}
print(*my_set, 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 set to access all characters except the first and last ones—that are the curly bracket, or braces, characters. For example, the expression print(str({1, 2, 3})[1:-1]) prints the list as "1, 2, 3" without enclosing square brackets.

my_set = {1, 2, 3}
# 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 set without brackets by combining the string.join() method on the separator string ', ' with a generator expression to convert each set element to a string using the str() built-in function. Specifially, the expression print(', '.join(str(x) for x in my_set)) prints my_set to the shell without enclosing brackets.

my_set = {1, 2, 3}
print(', '.join(str(x) for x in my_set))
# Output: 1, 2, 3

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

my_set = {1, 2, 3}
print('-'.join(str(x) for x in my_set))
# 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