Print a Set without brackets in Python

avatar

Borislav Hadzhiev

Last updated: Sep 2, 2022

banner

Photo from Unsplash

Print a Set without brackets in Python #

Use the str.join() method to print a set without brackets, e.g. print(','.join(str(item) for item in my_set)). The str.join() method will return a string containing the set's elements without brackets, with a comma separator.

main.py
# ✅ print set of strings without brackets set_of_str = {'one', 'two', 'three'} result = ','.join(set_of_str) print(result) # 👉️ 'one,two,three' # ----------------------------------------- # ✅ print set of integers without brackets set_of_int = {1, 2, 3} result = ','.join(str(item) for item in set_of_int) print(result) # 👉️ '1,2,3' # ----------------------------------------- # ✅ print list of sets without brackets list_of_sets = [{1, 2}, {3, 4}, {5, 6}] result = ','.join(','.join(str(element) for element in s) for s in list_of_sets) print(result) # 👉️ '1,2,3,4,5,6'

We used the str.join() method to print a set without brackets.

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

Note that the method raises a TypeError if there are any non-string values in the iterable.

If your set contains numbers or other types, convert all of the values to string before calling join().

main.py
set_of_int = {1, 2, 3} result = ','.join(str(item) for item in set_of_int) print(result) # 👉️ '1,2,3'

The example uses a generator expression to convert each integer in the set to a string.

Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we convert the current set element to a string and return the result.

The string the join() method is called on is used as the separator between the elements.

main.py
set_of_str = {'one', 'two', 'three'} result = ', '.join(set_of_str) print(result) # 👉️ 'one, two, three'

If you don't need a separator and just want to join the iterable's elements into a string, call the join() method on an empty string.

main.py
set_of_int = {1, 2, 3} result = ''.join(str(item) for item in set_of_int) print(result) # 👉️ '123'

If you need to print the set's elements without brackets and separated by spaces, call the str.join() method on a string containing a space.

main.py
set_of_int = {1, 2, 3} result = ' '.join(str(item) for item in set_of_int) print(result) # 👉️ '1 2 3'

If you need to print a list of set objects without brackets, use 2 calls to the str.join() method.

main.py
list_of_sets = [{1, 2}, {3, 4}, {5, 6}] result = ','.join(','.join(str(element) for element in s) for s in list_of_sets) print(result) # 👉️ '1,2,3,4,5,6'

The inner call to the join() method joins the items of the set of the current iteration.

We used the str() class to convert each number to a string.

The last step is to use the join() method to join the set objects in the list into a string with a comma separator.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.