*Memos:
- My post explains set functions (1).
- My post explains set functions (2).
- My post explains the shallow copy of the set with a tuple.
- My post explains the shallow and deep copy of the set with an iterator.
- My post explains a list and the list with indexing.
- My post explains a tuple.
- My post explains a dictionary (1).
- My post explains an iterator (1).
- My post explains a string.
- My post explains a byte string.
A set:
- is the unordered collection with zero or more elements. *Unordered means that the order of the elements in a set isn't kept so it doesn't guarantee that the order is always the same.
- shouldn't be huge not to get
MemoryError
. - doesn't allow duplicated elements (even with different types).
- is mutable so it can be changed. *add(), remove(), pop(), etc can be used to change a set.
- can have any mixed types of elements except the unhashable types e.g. a byte string(bytearray), list, set, dictionary, etc.
- cannot be enlarged with * and a number.
- can be created by
{}
, set() with or without a list, tuple, set, frozenset, dictionary, iterator, string, byte string(bytes or bytearray) or range() or a set comprehension. *Forset()
, the words type conversion are also suitable in addition to the word creation. - cannot be read or changed by indexing or slicing.
- can be continuously used through multiple variables.
- can be copied to refer to a different set.
A set is for non-huge data otherwise it gets MemoryError
.
{}
can create a set as shown below. *Be careful, the empty curlybraces {}
are an empty dictionary but not an empty set so use set() to create an empty set:
A = set() # Empty 1D set.
A = {} # It's an empty dictionary but not an empty set.
A = {10, 20, 30, 40, 50} # 1D set
A = {10, 30, 50, 10, 30, 50} # 1D set
A = {10, 30, (50, 10, 30, 50)} # 2D set
A = {10, 30, (50, 10, (30, 50))} # 3D set
A = {1, 1.0, 1.0+0.0j, True}
A = {'A', b'A', 2, 2.3, 2.3+4.5j, True, (2, 3), frozenset({2, 3}),
range(2, 3), iter([2, 3])}
# No error
A = {bytearray(b'Hello')}
# TypeError: unhashable type: 'bytearray'
A = {10, 30, [50, 10, 30, 50]}
A = {10, 30, (50, 10, [30, 50])}
# TypeError: unhashable type: 'list'
A = {10, 30, {50, 10, 30, 50}}
A = {10, 30, (50, 10, {30, 50})}
# TypeError: unhashable type: 'set'
A = {10, 30, {'A':'a', 'B': 'b', 'C':'c', 'D':'d'}}
A = {10, 30, (50, 10, {'A':'a', 'B':'b'})}
# TypeError: unhashable type: 'dict'
A = {10, 20, 30, 40, 50} * 3
# TypeError: unsupported operand type(s) for *: 'set' and 'int'
A set is the unordered collection with zero or more elements as shown below:
A = {10, 20, 30, 40, 50}
print(A)
# {50, 20, 40, 10, 30}
A = set()
print(A)
# set()
A set doesn't allow duplicated elements (even with different types) as shown below:
A = {10, 30, 50, 10, 30, 50}
print(A)
# {10, 50, 30}
A = {1, 1.0, 1.0+0.0j, True}
print(A)
# {1}
A set can have any mixed types of elements except a bytearray, set, list and dictionary as shown below:
A = {'A', b'A', 2, 2.3, 2.3+4.5j, True, (2, 3), frozenset({2, 3}),
range(2, 3), iter([2, 3])}
print(A)
# {True, 2, 2.3, frozenset({2, 3}),
# <list_iterator object at 0x000001F3B9E5F250>,
# b'A', (2.3+4.5j), (2, 3), 'A', range(2, 3)}
set()
can create a set with or without a list, tuple, set, frozenset, dictionary, iterator, string, byte string(bytes or bytearray) or range()
as shown below:
*Memos:
- The 1st argument is
iterable
(Optional-Default:set()
-Type:Iterable). *Don't useiterable=
.
A = set() # Empty set
print(A)
# set()
A = [10, 20, 30, 40, 50] # list
print(set(A))
# {40, 10, 50, 20, 30}
A = (10, 20, 30, 40, 50) # tuple
print(set(A))
# {40, 10, 50, 20, 30}
A = {10, 20, 30, 40, 50} # set
print(set(A))
# {50, 20, 40, 10, 30}
A = frozenset({10, 20, 30, 40, 50}) # frozenset
print(set(A))
# {50, 20, 40, 10, 30}
A = {'name': 'John', 'age': 36, 'gender': 'Male'} # dictionary
print(set(A))
print(set(A.keys()))
# {'gender', 'name', 'age'}
print(set(A.values()))
# {'John', 36, 'Male'}
print(set(A.items()))
# {('age', 36), ('name', 'John'), ('gender', 'Male')}
A = iter([10, 20, 30, 40, 50]) # iterator
print(set(A))
# {40, 10, 50, 20, 30}
A = 'Hello' # string
print(set(A))
# {'e', 'l', 'o', 'H'}
A = b'Hello' # bytes
print(set(A))
# {72, 108, 101, 111}
A = bytearray(b'Hello') # bytearray
print(set(A))
# {72, 108, 101, 111}
A = range(5) # range
print(set(A))
# {0, 1, 2, 3, 4}
A set comprehension can create a set as shown below:
A = {x**2 for x in range(6)}
print(A)
# {0, 1, 4, 9, 16, 25}
Be careful, a huge set gets MemoryError
as shown below:
A = range(100000000)
print(set(v))
# MemoryError
A set cannot be read or changed by indexing or slicing as shown below. *A del statement can still be used to remove one or more variables themselves:
A = {10, 20, 30, 40, 50, 60}
print(A[0], A[2:6])
# TypeError: 'set' object is not subscriptable
A = {10, 20, 30, 40, 50, 60}
A[0] = 100
A[2:6] = [200, 300]
# TypeError: 'set' object does not support item assignment
A = {10, 20, 30, 40, 50, 60}
del A[0], A[3:5]
# TypeError: 'set' object doesn't support item deletion
A = {10, 20, 30, 40, 50, 60}
del A
print(A)
# NameError: name 'A' is not defined
If you really want to read or change a tuple, use list() and set()
as shown below:
A = {10, 20, 30, 40, 50, 60}
A = list(A)
print(A[0], A[2:6])
# 50 [40, 10, 60, 30]
A[0] = 100
A[2:6] = [200, 300]
A = set(A)
print(A)
# {200, 100, 20, 300}
A = {10, 20, 30, 40, 50, 60}
A = list(A)
del A[0], A[3:5]
A = set(A)
print(A)
# {40, 10, 20}
A set can be continuously used through multiple variables as shown below:
A = B = C = {10, 20, 30} # Equivalent
# v1 = {10, 20, 30}
A.update({40, 50}) # v2 = v1
B.remove(30) # v3 = v2
C.pop()
print(A) # {20, 40, 10}
print(B) # {20, 40, 10}
print(C) # {20, 40, 10}
The variables A
and B
refer to the same set unless copied as shown below:
*Memos:
-
is
keyword can check ifA
andB
refer to the same set. -
set.copy(), copy.copy() and
set()
do shallow copy. *set.copy()
has no arguments. - copy.deepcopy() does deep copy.
-
copy.deepcopy()
should be used because it's safe, doing copy deeply whileset.copy()
,copy.copy()
andset()
aren't safe, doing copy shallowly.
import copy
A = {10, 20, 30}
B = A # B refers to the same set as A.
B.add(40) # Changes the same set as A.
# ↓↓
print(A) # {40, 10, 20, 30}
print(B) # {40, 10, 20, 30}
# ↑↑
print(A is B)
# True
B = A.copy() # B refers to the different set from A.
B = copy.copy(A)
B = copy.deepcopy(A)
B = set(A)
B.add(50) # Changes a different set from A.
print(A) # {40, 10, 20, 30}
print(B) # {40, 10, 50, 20, 30}
# ↑↑
print(A is B)
# False