Python 3 - Data types - Using a set

A set is a container that enables a way to retrieve unique data inside this container.

It means that if you want to be sure that only one occurrence is present in a container, you have to choose the set one.

For the form, we have to use the curly brackets to declare a set.

Let's see examples in detail with this tutorial of the Python 3 set data type.

Notice that the set container doesn't sort itself.
To sort it, just use the sorted(mySet) method (see example 8 below).
In the 8th example, we can see that the result is not a set anymore, but a list.
Indeed, the result is surrounded by square brackets and not curly brackets.

I define a function to help in this exercise, I called it setManager() and takes two parameters.

I am going to show how to use the set container by a classical way and also with their methods.

Using set in a classical way

Code

# BEGIN
def setManager(name, theSet):
    print(name)
    print(str(theSet) + "\n")
# END

mySet1 = {"Hello", "World!", "Hello"}
setManager("Set1 --> declaring a set of strings (unique of course).", mySet1)

mySet2 = set("aAaBb cCCC Dd")
setManager("Set2 --> retrieve only one letter of each (not sorted).", mySet2)

mySet3 = set("abBCDedFE")
setManager("Set3 --> retrieve only one letter of each (not sorted).", mySet3)

mySet4 = mySet2 | mySet3
setManager("Set4 --> retrieve all different letters from Set2 and Set3 (not sorted).", mySet4)

mySet5 = mySet2 & mySet3
setManager("Set5 --> retrieve all same letters from Set2 and Set3 (not sorted).", mySet5)

mySet6 = mySet2 - mySet3
setManager("Set6 --> retrieve all letters in Set2 but not in Set3 (not sorted).", mySet6)

mySet7 = mySet2 ^ mySet3
setManager("Set7 --> retrieve all letters from Set2 and Set3 but not from both (not sorted).", mySet7)

mySet8 = sorted(mySet2)
setManager("Set8 --> retrieve all letters sorted.", mySet8)

Result

Set1 --> declaring a set of strings (unique of course).
{'World!', 'Hello'}

Set2 --> retrieve only one letter of each (not sorted).
{'a', 'A', 'c', 'B', 'D', ' ', 'C', 'b', 'd'}

Set3 --> retrieve only one letter of each (not sorted).
{'a', 'C', 'B', 'e', 'D', 'F', 'b', 'E', 'd'}

Set4 --> retrieve all different letters from Set2 and Set3 (not sorted).
{'a', 'A', 'c', 'B', 'e', 'D', ' ', 'F', 'C', 'b', 'E', 'd'}

Set5 --> retrieve all same letters from Set2 and Set3 (not sorted).
{'a', 'C', 'B', 'D', 'b', 'd'}

Set6 --> retrieve all letters in Set2 but not in Set3 (not sorted).
{'A', ' ', 'c'}

Set7 --> retrieve all letters from Set2 and Set3 but not from both (not sorted).
{'A', ' ', 'e', 'F', 'c', 'E'}

Set8 --> retrieve all letters sorted.
[' ', 'A', 'B', 'C', 'D', 'a', 'b', 'c', 'd']

Using the set methods

Code

# BEGIN
def setManager(name, theSet):
    print(name)
    print(str(theSet) + "\n")
# END

mySet = set("abcdefghij")
setManager("Initialization", mySet)

mySet.add("Hello World!")
setManager("add() --> add an element", mySet)

mySet2 = mySet.copy()
setManager("copy() --> return a copy not linked with the main set", mySet2)

mySet2.clear()
setManager("clear() --> remove all elements from mySet2", mySet2)

setManager("Testing --> mySet is still full", mySet)

mySet3 = set("abcDEfghijKLmno")
setManager("Initializing mySet3", mySet3)

mySet = mySet.difference(mySet3)
setManager("difference() --> retrieve all elements present in mySet but not in mySet3", mySet)

mySet.difference_update(mySet3)
setManager("difference_update() --> same as difference() but without a new assignment", mySet)

mySet.discard("e")
setManager("discard() --> remove an element if it is found in the set", mySet)

setManager("Clear elements in mySet", mySet.clear())
setManager("Clear elements in mySet3", mySet3.clear())

mySet = set("abcdefgh")
setManager("Initialize mySet", mySet)

mySet3 = set("fghijk")
setManager("Initialize mySet3", mySet3)

mySet = mySet.intersection(mySet3)
setManager("intersection() --> retrieve only elements present in mySet and mySet3", mySet)

mySet.intersection_update(mySet3)
setManager("intersection_update() --> same as intersection() but without a new assignment", mySet)

myBoolean = mySet.isdisjoint(mySet3)
setManager("isdisjoint() --> return True if there is no intersection between mySet and mySet3, otherwise return False", myBoolean)

setManager("Elements in mySet", mySet)
setManager("Elements in mySet3", mySet3
           )
mySub = mySet.issubset(mySet3)
setManager("issubset() --> test if all elements of mySet are present in mySet3", mySub)

mySuper = mySet.issuperset(mySet3)
setManager("issuperset() --> test if all elements of mySet3 are present in mySet", mySuper)

mySet.pop()
setManager("pop() --> remove the last item of mySet", mySet)

mySet.remove("f")
setManager("remove() --> remove the element specified in parameter, if the element doesn't exist, return a KeyError", mySet)


mySet.add("f")
mySet.add("i")
mySet.add("k")
mySet.add("l")
setManager("Elements in mySet", mySet)
setManager("Elements in mySet3", mySet3)

mySet4 = mySet.symmetric_difference(mySet3)
setManager("symmetric_difference() --> return all elements present in exactly one set but not in both", mySet4)

mySet.symmetric_difference_update(mySet3)
setManager("symmetric_difference() --> same as symmetric_difference but without a new assignment", mySet)

mySet = mySet.union(mySet3)
setManager("union() --> add all elements of mySet3 in mySet", mySet)

mySet.update(set("Hello"))
setManager("update() --> add all different characters in the current set", mySet)

Result

Initialization
{'a', 'c', 'b', 'e', 'd', 'g', 'f', 'i', 'h', 'j'}

add() --> add an element
{'a', 'c', 'b', 'e', 'd', 'g', 'f', 'i', 'h', 'j', 'Hello World!'}

copy() --> return a copy not link with the main set
{'a', 'c', 'b', 'e', 'd', 'g', 'f', 'i', 'h', 'j', 'Hello World!'}

clear() --> remove all elements from mySet2
set()

Testing --> mySet is still full
{'a', 'c', 'b', 'e', 'd', 'g', 'f', 'i', 'h', 'j', 'Hello World!'}

Initializing mySet3
{'a', 'c', 'b', 'E', 'D', 'g', 'f', 'i', 'h', 'K', 'j', 'm', 'L', 'o', 'n'}

difference() --> retrieve all elements present in mySet but not in mySet3
{'Hello World!', 'e', 'd'}

difference_update() --> same as difference() but without a new assignment
{'Hello World!', 'e', 'd'}

discard() --> remove an element if it is found in the set
{'Hello World!', 'd'}

Clear elements in mySet
None

Clear elements in mySet3
None

Initialize mySet
{'a', 'c', 'b', 'e', 'd', 'g', 'f', 'h'}

Initialize mySet3
{'g', 'f', 'i', 'h', 'k', 'j'}

intersection() --> retrieve only elements present in mySet and mySet3
{'h', 'g', 'f'}

intersection_update() --> same as intersection() but without a new assignment
{'h', 'g', 'f'}

isdisjoint() --> return True if there is no intersection between mySet and mySet3, otherwise return False
False

Elements in mySet
{'h', 'g', 'f'}

Elements in mySet3
{'g', 'f', 'i', 'h', 'k', 'j'}

issubset() --> test if all elements of mySet are present in mySet3
True

issuperset() --> test if all elements of mySet3 are present in mySet
False

pop() --> remove the last item of mySet
{'g', 'f'}

remove() --> remove the element specified in parameter, if the element doesn't exist, return a KeyError
{'g'}

Elements in mySet
{'g', 'f', 'i', 'k', 'l'}

Elements in mySet3
{'g', 'f', 'i', 'h', 'k', 'j'}

symmetric_difference() --> return all elements present in exactly one set but not in both
{'h', 'j', 'l'}

symmetric_difference() --> same as symmetric_difference but without a new assignment
{'h', 'j', 'l'}

union() --> add all elements of mySet3 in mySet
{'g', 'f', 'i', 'h', 'k', 'j', 'l'}

update() --> add all different characters in the current set
{'e', 'g', 'f', 'i', 'h', 'k', 'j', 'l', 'o', 'H'}

An interesting data type. smiley

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.