Sets in Python

Sets are data structures, similar to lists or dictionaries. They are created using curly braces, or the set function. They share some functionality with lists, such as the use of in to check whether they contain a particular item.

Example:

num_set = {1, 2, 3, 4, 5}
word_set = set(["spam", "eggs", "sausage"])

print(3 in num_set)
print(0 in num_set)
print("spam" not in word_set)
print("bread" not in word_set)
print("spam" in word_set)

The terminal output will be:

ddn_ro@linux:~/Desktop$ python file.py
True
False
False
True
True
ddn_ro@linux:~/Desktop$

Note:
To create an empty set, you must use set(), as {} creates an empty dictionary.

Sets differ from lists in several ways, but share several list operations such as len.
They are unordered, which means that they can’t be indexed.
They cannot contain duplicate elements.
Due to the way they’re stored, it’s faster to check whether an item is part of a set, rather than part of a list.
Instead of using append to add to a set, use add.
The method remove removes a specific element from a set; pop removes an arbitrary element.

Example:

nums = {1, 2, 1, 3, 1, 4, 5, 6}
print(nums)
nums.add(-7)
nums.remove(3)
print(nums)

The terminal output will be:

ddn_ro@linux:~/Desktop$ python file.py
set([1, 2, 3, 4, 5, 6])
set([1, 2, 4, 5, 6, -7])
ddn_ro@linux:~/Desktop$

Note:
Basic uses of sets include membership testing and the elimination of duplicate entries.

Sets can be combined using mathematical operations.
The union operator | combines two sets to form a new one containing items in either.
The intersection operator & gets items only in both.
The difference operator - gets items in the first set but not in the second.
The symmetric difference operator ^ gets items in either set, but not both.

Example:

first = {1, 2, 3, 4, 5, 6}
second = {4, 5, 6, 7, 8, 9}

print(first | second)
print(first & second)
print(first - second)
print(second - first)
print(first ^ second)

The result will be:

ddn_ro@linux:~/Desktop$ python file.py
set([1, 2, 3, 4, 5, 6, 7, 8, 9])
set([4, 5, 6])
set([1, 2, 3])
set([8, 9, 7])
set([1, 2, 3, 7, 8, 9])
ddn_ro@linux:~/Desktop$

Use a dictionary when you need a logical association between a key:value pair.
Use a dictionary when you need fast lookup for your data, based on a custom key.
Use a dictionary when your data is being constantly modified. Remember, dictionaries are mutable.
Use lists if you have a collection of data that does not need random access.
Use lists when you need a simple, iterable collection that is modified frequently.
Use a set if you need uniqueness for the elements.
Use tuples when your data cannot change.

Note:
Many times, a tuple is used in combination with a dictionary, for example, a tuple might represent a key, because it’s immutable.

Leave a Reply