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, the other 3 are List, Tuple, and Dictionary, all with different qualities and usage.
A set is a collection which is unordered, unchangeable*, and unindexed.
myset = {"apple", "banana", "cherry"}
* Note: Set items are unchangeable, but you can remove items and add new items.
Sets are written with curly brackets.
Note: Sets are unordered, so you cannot be sure in which order the items will appear.
Set Items
Set items are unordered, unchangeable, and do not allow duplicate values.
Unordered
Unordered means that the items in a set 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
Set items are unchangeable, meaning that we cannot change the items after the set has been created.
Once a set is created, you cannot change its items, but you can remove items and add new items.
Duplicates Not Allowed
Sets cannot have two items with the same value.
Get the Length of a Set
To determine how many items a set has, use the len()
function.
Set Items - Data Types
Set items can be of any data type:
A set can contain different data types:
type()
From Python's perspective, sets are defined as objects with the data type 'set':
<class 'set'>
The set() Constructor
It is also possible to use the set() constructor to make a set.
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, unchangeable*, and unindexed. No duplicate members.
Dictionary is a collection which is ordered** and changeable. No duplicate members.
*Set items are unchangeable, but you can remove items and add new items.
**As of Python version 3.7, dictionaries are ordered. In Python 3.6 and earlier, dictionaries are unordered.
When choosing a collection type, it is useful to understand the properties of that type. Choosing the right type for a particular data set could mean retention of meaning, and, it could mean an increase in efficiency or security.
Access Items
You cannot access items in a set by referring to an 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.
Change Items
Once a set is created, you cannot change its items, but you can add new 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.
Add Sets
To add items from another set into the current set, use the update()
method.
Add Any Iterable
The object in the update()
method does not have to be a set, it can be any iterable object (tuples, lists, dictionaries etc.).
Remove Item
To remove an item in a set, use the remove()
, or the discard()
method.
Note: If the item to remove does not exist, remove()
will raise an error.
Note: If the item to remove does not exist, discard()
will NOT raise an error.
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 that gets removed.
The return value of the pop()
method is the removed item.
Note: Sets are unordered, so when using the pop()
method, you do not know which item that gets removed.
Loop Items
You can loop through the set items by using a for
loop:
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, or the update()
method that inserts all the items from one set into another:
Note: Both union()
and update()
will exclude any duplicate items.
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.
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.
Set Methods
Python has a set of built-in methods that you can use on sets.
Last updated