Python Sets
A set lets a single variable hold several items at once.
Python has four built-in collection types: List, Tuple, Dictionary and Set. Each one behaves differently and suits different jobs.
What makes a set special is that it is unordered, unchangeable* and unindexed.
Note: The "unchangeable" part applies to the existing items: you are still free to add new ones or remove old ones.
To write a set, put its items between curly brackets.
Example
Build a set and print it:
Output: complete Python program
thisset = {"apple", "banana", "cherry"}
print(thisset)
Set intro
myset = {"apple", "banana", "cherry"}
Set Items
Three rules describe set items: they have no order, they cannot be changed, and no value can appear twice.
Unordered
A set keeps its items in no particular sequence.
Because of that, they may show up in a different arrangement each time you use the set, and you cannot reach one through an index or a key.
Unchangeable
After a set exists, the items inside it stay as they are; you cannot edit any of them.
The set itself can still grow or shrink, though, since adding and removing items is allowed.
Duplicates Not Allowed
Every value in a set must be unique, so two equal items can never sit side by side.
Note: To a set, True and 1 count as one value, so the second is dropped as a duplicate.
Note: The same goes for False and 0, which a set also treats as one value.
Example
The second "apple" is simply dropped:
Output: complete Python program
thisset = {"apple", "banana", "cherry", "apple"}
print(thisset)
Example
Here True and 1 collapse into one item:
Output: complete Python program
thisset = {"apple", "banana", "cherry", True, 1, 2}
print(thisset)
Example
Here False and 0 collapse into one item:
Output: complete Python program
thisset = {"apple", "banana", "cherry", False, True, 0}
print(thisset)
Get the Length of a Set
The len() function tells you how many items a set holds.
Example
Count the items in a set:
Output: complete Python program
thisset = {"apple", "banana", "cherry"}
print(len(thisset))
Set Items - Data Types
Set items are not limited to one data type.
You can even mix several types in one set:
Example
One set of strings, one of integers and one of booleans:
Output: complete Python program
set1 = {"apple", "banana", "cherry"}
set2 = {1, 5, 7, 9, 3}
set3 = {True, False, False}
Example
Strings, integers and booleans together in one set:
Output: complete Python program
set1 = {"abc", 34, True, 40, "male"}
type()
Python treats every set as an object, and the name of its data type is 'set':
Example
Ask Python what type a set is:
Output: complete Python program
myset = {"apple", "banana", "cherry"}
print(type(myset))
The set() Constructor
Curly brackets are not the only option: the set() constructor builds a set too.
Example
Build a set with the set() constructor:
Output: complete Python program
thisset = set(("apple", "banana", "cherry")) # note the double round-brackets
print(thisset)
Python Collections (Arrays)
The Python language gives you four collection data types:
- List keeps its order and can be changed. Duplicate members are fine.
- Tuple keeps its order but cannot be changed. Duplicate members are fine.
- Set has no order or index, and its items are unchangeable*. Every member is unique.
- Dictionary keeps its order** and can be changed. Every member is unique.
*You cannot edit existing set items, although adding and removing them is allowed.
**Dictionaries became ordered in Python version 3.7; up to and including Python 3.6 they were unordered.
Knowing what each type can and cannot do helps you pick well. The right choice for your data can preserve its meaning and may make your program more efficient or secure.