Python Program to Print Elements of Set (5 Different Ways)

In this article, we will learn about python program to print elements of set with examples. We will see different ways in which we can print set elements in python with or without maintaining order of elements in set.

Getting Started

We can print elements of set in multiple ways –

  1. Print Set Using print() Method
  2. Print Set Using For Loop
  3. Print Set Using Set Comprehension
  4. Print Set By Converting to String
  5. Print Set By Using map()

Let’s see how to print set using above methods one by one –

We can print elements of set using print() method as shown below –

  • Method 1:

    By passing name of set in print() method –

    set1 = {'a','b','c','d','e','f'}
    print(set1)
    

    Output:

    {'b', 'a', 'f', 'd', 'c', 'e'}
    

    set1 is name of python set which we passed as argument in print method.

    Let’s see another example –

     
    set2 = set(['a','b','c','d','e','f'])
    print(set2)
    

    Output:

     
    {'e', 'a', 'd', 'b', 'f', 'c'}
    

    set2 is name of python set which we passed as argument in print method.

  • Method 2:

    Using * symbol with name of set as parameter to print() method prints elements of set. For example,

    sampleSet = set([11, 12, 13, 14, 15])
    print(*sampleSet)
    

    Output:

    11 12 13 14 15
    

    Notice that we have used * symbol as prefix to name of set.

  • Method 3:

    We can also print elements of set separated by commas. We can do so using separator , in print() method. For example,

    sampleSet = set([11, 12, 13, 14, 15])
    print(*sampleSet, sep = ", ")
    

    Output:

    11, 12, 13, 14, 15
    

    Notice that we have used sep = “, “ as second parameter in print() method.

  • Method 4:

    We can also print elements of set in new line using separator “\n”. For example,

    sampleSet = set([11, 12, 13, 14, 15])
    print(*sampleSet, sep = "\n")
    

    Output:

    11
    12
    13
    14
    15
    

Using for Loop, we can print elements of set as shown below –

set2 = set(['a','b','c','d','e','f'])
for item in set2:
    print(item) 

Output:

b
f
d
c
a
e

At each iteration, we get one item present in set, stored in variable item. Then, we print it using print() method.

Using set comprehension, we can print all elements of set as below –

set2 = set(['a','b','c','d','e','f'])
[print(x) for x in set2]

Output:

b
e
f
d
a
c

If elements of set is either number or string, we can convert to string and print it.

  1. If set has string elements, we can simply join them using .join() method. Then, print elements as shown below –
     
    sampleSet = set(["Hello", "Hi", "Tutorialwing"])
    print(' '.join(sampleSet)) 
    

    Output:

    Tutorialwing Hi Hello
    
  2. If set has integer elements, we can convert them into string using str method and print elements. For example,

    sampleSet = set([13, 23, 33, 43, 53])
    print(str(sampleSet)[1:-1])
    

    Output:

    33, 43, 13, 53, 23
    

If set does not contain string elements, we can convert them into string using map and print them. For example,

sampleSet = set([1, 2, 3, 4, 5])
print(' '.join(map(str, sampleSet)))

Output:

1 2 3 4 5

That’s how we can write python program to print elements of set.

Learn more about set at official doc.

Leave a Reply