Set in Python (1)
Super Kai (Kazuya Ito)

Super Kai (Kazuya Ito) @hyperkai

About: I'm a web developer. Buy Me a Coffee: ko-fi.com/superkai SO: stackoverflow.com/users/3247006/super-kai-kazuya-ito X(Twitter): twitter.com/superkai_kazuya FB: facebook.com/superkai.kazuya

Joined:
Oct 21, 2021

Set in Python (1)

Publish Date: Jun 1
0 0

Buy Me a Coffee

*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'
Enter fullscreen mode Exit fullscreen mode

A set is an unordered collection as shown below:

A = {10, 20, 30, 40, 50}

print(A) # {50, 20, 40, 10, 30}
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode
A = {1, 1.0, 1.0+0.0j, True}

print(A) # {1}
Enter fullscreen mode Exit fullscreen mode

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])]
Enter fullscreen mode Exit fullscreen mode

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()
Enter fullscreen mode Exit fullscreen mode
A = [10, 20, 30, 40, 50] # List

print(set(A)) # {40, 10, 50, 20, 30}
Enter fullscreen mode Exit fullscreen mode
A = (10, 20, 30, 40, 50) # Tuple

print(set(A)) # {40, 10, 50, 20, 30}
Enter fullscreen mode Exit fullscreen mode
A = {10, 20, 30, 40, 50} # Set

print(set(A)) # {50, 20, 40, 10, 30}
Enter fullscreen mode Exit fullscreen mode
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')}
Enter fullscreen mode Exit fullscreen mode
A = iter([10, 20, 30, 40, 50]) # Iterator

print(set(A)) # {40, 10, 50, 20, 30}
Enter fullscreen mode Exit fullscreen mode
v = 'Hello' # String

print(set(v)) # {'l', 'H', 'o', 'e'}
Enter fullscreen mode Exit fullscreen mode
v = range(5)

print(set(v)) # {0, 1, 2, 3, 4}
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode

Be careful, a huge set gets MemoryError as shown below:

v = range(100000000)

print(set(v)) # MemoryError
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
A = {10, 20, 30, 40, 50, 60}

del A

print(A) # NameError: name 'A' is not defined
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode
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}
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode

The variables A and B refer to the same set unless copied as shown below:

*Memos:

  • is keyword can check if A and B 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 while copy() 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
Enter fullscreen mode Exit fullscreen mode

Comments 0 total

    Add comment