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 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. *For set(), 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'
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode
A = set()

print(A)
# set()
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 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)}
Enter fullscreen mode Exit fullscreen mode

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 use iterable=.
A = set() # Empty set

print(A)
# 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 = frozenset({10, 20, 30, 40, 50}) # frozenset

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()))
# {'gender', 'name', 'age'}

print(set(A.values()))
# {'John', 36, 'Male'}

print(set(A.items()))
# {('age', 36), ('name', 'John'), ('gender', 'Male')}
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
A = 'Hello' # string

print(set(A))
# {'e', 'l', 'o', 'H'}
Enter fullscreen mode Exit fullscreen mode
A = b'Hello' # bytes

print(set(A))
# {72, 108, 101, 111}
Enter fullscreen mode Exit fullscreen mode
A = bytearray(b'Hello') # bytearray

print(set(A))
# {72, 108, 101, 111}
Enter fullscreen mode Exit fullscreen mode
A = range(5) # range

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

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

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

A = range(100000000)

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

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

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

A = list(A)

del A[0], A[3:5]

A = set(A)

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, 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}
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.
  • 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 while set.copy(), copy.copy() and set() 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
Enter fullscreen mode Exit fullscreen mode

Comments 0 total

    Add comment