*Memos:
- My post explains the useful functions for a set (1).
- My post explains the useful functions for a set (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, the dictionary with keying and shallow copy.
- My post explains an iterator (1).
- My post explains variable assignment.
A set:
- is an unordered collection to use non-huge data not to get
MemoryError
. - doesn't allow duplicated elements (even with different types).
- is mutable so it can be changed.
- can have any mixed types of elements except a set, list and dictionary.
- cannot be enlarged with * and a number.
- can be created by
{}
, set() with or without a list, tuple, set, dictionary, iterator, string or range() or by a set comprehension. - cannot be accessed and 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', 2, 2.3, 2.3+4.5j, True, (2, 3), iter([2, 3])}
# No error
A = {10, 30, {50, 10, 30, 50}}
A = {10, 30, (50, 10, {30, 50})}
# TypeError: unhashable type: 'set'
A = {10, 30, [50, 10, 30, 50]}
A = {10, 30, (50, 10, [30, 50])}
# TypeError: unhashable type: 'list'
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 an unordered collection as shown below:
A = {10, 20, 30, 40, 50}
print(A) # {50, 20, 40, 10, 30}
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 set, list and dictionary as shown below:
A = ['a', 2, 2.3, 2.3+4.5j, True, (2, 3)]
print(A) # ['a', 2, 2.3, (2.3+4.5j), True, (2, 3), iter([2, 3])]
set()
can create a set with or without a list, tuple, set, dictionary, iterator, string or range()
as shown below:
*Memos:
- The 1st argument is
iterable
(Optional-Type:iterable
). - Don't use
iterable=
.
v = set() # Empty set
print(v) # 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 = {'name': 'John', 'age': 36, 'gender': 'Male'} # Dictionary
print(set(A))
print(set(A.keys()))
# {'name', 'age', 'gender'}
print(set(A.values()))
# {'John', 'Male', 36}
print(set(A.items()))
# {('age', 36), ('gender', 'Male'), ('name', 'John')}
A = iter([10, 20, 30, 40, 50]) # Iterator
print(set(A)) # {40, 10, 50, 20, 30}
v = 'Hello' # String
print(set(v)) # {'l', 'H', 'o', 'e'}
v = range(5)
print(set(v)) # {0, 1, 2, 3, 4}
A set comprehension can create a set as shown below:
v = {x**2 for x in range(6)}
print(v) # {0, 1, 4, 9, 16, 25}
Be careful, a huge set gets MemoryError
as shown below:
v = range(100000000)
print(set(v)) # MemoryError
You cannot access and change a set by indexing or slicing as shown below. *A del statement can still be used to remove a variable itself:
A = {10, 20, 30, 40, 50, 60}
A[0] = 100
# A[-6] = 100
A[2:6] = [200, 300]
# TypeError: 'set' object does not support item assignment
A = {10, 20, 30, 40, 50, 60}
del A[0]
# del A[-6]
del 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 access and change a tuple, use list() and set() as shown below:
A = {10, 20, 30, 40, 50, 60}
A = list(A) # Here
A[0] = 100
# A[-6] = 100
A[2:6] = [200, 300]
A = set(A) # Here
print(A) # {200, 100, 20, 300}
A = {10, 20, 30, 40, 50, 60}
A = list(A) # Here
del A[0]
# del A[-6]
del A[3:5]
A = set(A) # Here
print(A) # {40, 10, 20}
A set can be continuously used through multiple variables as shown below:
A = B = C = {10, 20} # Equivalent
# A = {10, 20}
A.add(30) # B = A
B.add(40) # C = B
C.add(50)
print(A) # {40, 10, 50, 20, 30}
print(B) # {40, 10, 50, 20, 30}
print(C) # {40, 10, 50, 20, 30}
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. - copy() can do shallow copy. *There are no arguments.
- deepcopy() does deep copy. *There are no arguments.
-
deepcopy()
should be used because it's safe, doing copy deeply whilecopy()
isn't safe, doing copy shallowly.
from copy import deepcopy
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 = deepcopy(A)
B.add(50) # Changes a different set from A.
print(A) # {40, 10, 20, 30}
print(B) # {40, 10, 50, 20, 30}
# ↑↑
print(v1 is v2) # False