Python - tutorial - 06/14

sets

Revision:


Python sets

Sets are used to store multiple items in a single variable. Set is one of 4 built-in data types in Python used to store collections of data.
A set is a collection which is both unordered and unindexed.
Sets are written with curly brackets .

Example 1: create a set:

        thisset = {"apple", "banana", "cherry"}
        print(thisset)
    

Set items are "unordered", "unchangeable", and "do not allow duplicate values".

Unordered: the items do not have a defined order. Set items can appear in a different order every time you use them, and cannot be referred to by index or key.

Unchangeable: we cannot change, add or remove items after the set has been created. Once a set is created, you cannot change its items, but you can add new items.

Duplicates not allowed: sets cannot have two items with the same value.

Example 2: duplicate values will be ignored:

        thisset = {"apple", "banana", "cherry", "apple"}
        print(thisset) # {'banana', 'cherry', 'apple'}
    

Get the length of a set: to determine how many items a set has, use the len() method.

Example 3: get the number of items in a set:

        thisset = {"apple", "banana", "cherry"}
        print(len(thisset)) # 3
    

Set items - data types: set items can be of any data type. A set can contain different data types.

Example 4: string, int and boolean data types:

        set1 = {"apple", "banana", "cherry"}
        set2 = {1, 5, 7, 9, 3}
        set3 = {True, False, False}
    

Example 5: a set with strings, integers and boolean values:

        set1 = {"abc", 34, True, 40, "male"}
    

type(): from Python's perspective, sets are defined as objects with the data type 'set'.

The set() constructor can be used to make a set.

Example 6: using the set() constructor to make a set:

        thisset = set(("apple", "banana", "cherry")) # note the double round-brackets
        print(thisset)
    

Python collections (arrays): There are four collection data types in the Python programming language:

"List" is a collection which is ordered and changeable. Allows duplicate members.
"Tuple" is a collection which is ordered and unchangeable. Allows duplicate members.
"Set" is a collection which is unordered and unindexed. No duplicate members.
"Dictionary" is a collection which is ordered and changeable. No duplicate members.


Python - access set items

Access set items: you cannot access items in a set by referring to the index or a key. But you can loop through the set items using a "for" loop, or ask if a specified value is present in a set, by using the "in" keyword.

Example 7: loop through the set, and print the values:

        thisset = {"apple", "banana", "cherry"}
        for x in thisset:
            print(x) # cherry apple banana
    

Example 8: check if "banana" is present in the set:

        thisset = {"apple", "banana", "cherry"}
        print("banana" in thisset) # True
    


Python - add set items

Add items: Once a set is created, you cannot change its items, but you can add new items. To add one item to a set use the add() method.

Example 9: add an item to a set, using the add() method:

        thisset = {"apple", "banana", "cherry"}
        thisset.add("orange")
        print(thisset) #{'cherry', 'apple', 'orange', 'banana'}
    

Add sets: to add items from another set into the current set, use the update() method.

Example 10: add elements from "tropical" and "thisset" into "newset":

        thisset = {"apple", "banana", "cherry"}
        tropical = {"pineapple", "mango", "papaya"}
        thisset.update(tropical)
        print(thisset) # {'apple', 'mango', 'cherry', 'pineapple', 'banana', 'papaya'}
    

Add any iterable: the object in the update() method does not have be a set, it can be any iterable object (tuples, lists, dictionaries etc.).

Example 11: add elements of a list to at set:

        thisset = {"apple", "banana", "cherry"}
        mylist = ["kiwi", "orange"]
        thisset.update(mylist) # {'banana', 'cherry', 'apple', 'orange', 'kiwi'}
        print(thisset)
    


Python - remove set items

Remove item: to remove an item in a set, use the remove() method or the discard() method. If the item to remove does not exist, remove() will raise an error. If the item to remove does not exist, discard() will NOT raise an error.

Example 12: remove "banana" by using the remove() method:

        thisset = {"apple", "banana", "cherry"}
        thisset.remove("banana")
        print(thisset)
    

Example 13: Remove "banana" by using the discard() method:

        thisset = {"apple", "banana", "cherry"}
        thisset.discard("banana")
        print(thisset)
    

You can also use the pop() method to remove an item, but this method will remove the last item. Remember that sets are unordered, so you will not know what item gets removed. The return value of the pop() method is the removed item.

Example 14: remove the last item by using the pop() method:

        thisset = {"apple", "banana", "cherry"}
        x = thisset.pop()
        print(x)
        print(thisset)
    

The clear() method empties the set.

The"del" keyword will delete the set completely.


Python - loop sets

Loop items: you can loop through the set items by using a "for" loop.

Example 15: loop through the set, and print the values:

        thisset = {"apple", "banana", "cherry"}
        for x in thisset:
            print(x)
    


Python - join sets

Join two sets: There are several ways to join two or more sets in Python. You can use:

the union() method that returns a new set containing all items from both sets,
the update() method that inserts all the items from one set into another.
Both union() and update() will exclude any duplicate items.

Example 16: the union() method returns a new set with all items from both sets:

        set1 = {"a", "b" , "c"}
        set2 = {1, 2, 3}
        set3 = set1.union(set2)
        print(set3) # {'a', 3, 2, 'c', 'b', 1}
    

Example 17: the update() method inserts the items in set2 into set1:

        set1 = {"a", "b" , "c"}
        set2 = {1, 2, 3}
        set1.update(set2)
        print(set1) # {'b', 3, 'c', 2, 'a', 1}
    

Keep ONLY the duplicates: the intersection_update() method will keep only the items that are present in both sets. The intersection() method will return a new set, that only contains the items that are present in both sets.

Example 18: keep the items that exist in both set x, and set y:

        x = {"apple", "banana", "cherry"}
        y = {"google", "microsoft", "apple"}
        x.intersection_update(y)
        print(x) # {'apple'}
    

Example 19: return a set that contains the items that exist in both set x, and set y:

        x = {"apple", "banana", "cherry"}
        y = {"google", "microsoft", "apple"}
        z = x.intersection(y)
        print(z) # {'apple'}
    

Keep all, but NOT the duplicates: the symmetric_difference_update() method will keep only the elements that are NOT present in both sets. The symmetric_difference() method will return a new set, that contains only the elements that are NOT present in both sets.

Example 20: keep the items that are not present in both sets:

        x = {"apple", "banana", "cherry"}
        y = {"google", "microsoft", "apple"}
        x.symmetric_difference_update(y)
        print(x) # {'google', 'banana', 'microsoft', 'cherry'}
    

Example 21: return a set that contains all items from both sets, except items that are present in both:

        x = {"apple", "banana", "cherry"}
        y = {"google", "microsoft", "apple"}
        z = x.symmetric_difference(y)
        print(z) # {'google', 'banana', 'microsoft', 'cherry'}
    


Python - set methods

Set methods: Python has a set of built-in methods that you can use on sets.

Method - Description
add() - Adds an element to the set
clear() - Removes all the elements from the set
copy() sans-serif Returns a copy of the set
difference() - Returns a set containing the difference between two or more sets
difference_update() - Removes the items in this set that are also included in another, specified set
discard() - Remove the specified item
intersection() - Returns a set, that is the intersection of two other sets
intersection_update() - Removes the items in this set that are not present in other, specified set(s)
isdisjoint() - Returns whether two sets have a intersection or not
issubset() - Returns whether another set contains this set or not
issuperset() - Returns whether this set contains another set or not
pop() - Removes an element from the set
remove() - Removes the specified element
symmetric_difference() - Returns a set with the symmetric differences of two sets
symmetric_difference_update() - inserts the symmetric differences from this set and another
union() - Return a set containing the union of sets
update() - Update the set with the union of this set and others