DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

Set in Python (1)

Buy Me a Coffee

*Memo:

  • My post explains a set (2).
  • My post explains a set (3).
  • My post explains a frozenset (1).
  • My post explains set and frozenset functions (1).
  • My post explains a set comprehension.
  • 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 bytes.
  • My post explains a bytearray.
  • My post explains a range (1).

A set:

  • is the unordered mutable(unhashable) collection of zero or more elements whose type is set:
    • Unordered means that the order of each element in a set isn't kept so it doesn't guarantee that the order is always the same.
    • Mutable(Unhashable) means the elements of a set can be changed:
  • doesn't allow duplicated elements (even with different types).
  • can have the hashable types of elements:
    • A hashable type is the type whose value cannot be changed like str, bytes, int, float, complex, bool, tuple, frozenset, range or iterator.
  • cannot have the unhashable types of elements:
    • A unhashable type is the type whose value can be changed like bytearray, list, set or dict.
  • can be used with len() to get the length.
  • is False if it's empty.
  • can be checked if a specific element is in the set with in keyword.
  • can be checked if the set is referred to by two variables with is keyword.
  • cannot be enlarged with * and a number.
  • cannot be huge because it gets MemoryError.
  • can be iterated with a for statement.
  • can be created by {} with or without ',', set() with or without an iterable or a set comprehension:
    • For set(), the words type conversion are also suitable in addition to the word creation.
  • cannot be read by indexing and slicing.
  • cannot be changed by indexing, slicing and a del statement.
  • can be unpacked with an assignment and for statement, function and * but not with **.
  • can be continuously used through multiple variables.
  • with a frozenset can be shallow-copied by set.copy(), copy.copy() and set().
  • with a frozenset can be deep-copied by copy.deepcopy().

{} with or without ',' can create a set as shown below:

*Memo:

  • 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 = {}                                                      # dict not set
A = {10, 20, 30, 40, 50}                                    # 1D set
A = {10, 20, 30, 10, 20, 30}                                # 1D set
A = {10, 20, 30, 40, frozenset({50, 60, 70, 80})}           # 2D set
A = {frozenset({10, 20, 30, 40}),                           # 2D set
     frozenset({50, 60, 70, 80})}
A = {frozenset({10, 20, 30, 40}),                           # 3D set
     frozenset({frozenset({50, 60}), frozenset({70, 80})})}
A = {frozenset({frozenset({10, 20}), frozenset({30, 40})}), # 3D set
     frozenset({frozenset({50, 60}), frozenset({70, 80})})}
# No error

A = {0, 0.0, 0.0+0.0j, False}
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])}
for x in {0, 1, 2, 3, 4}: pass
for x in {frozenset({10, 20, 30, 40}),
          frozenset({50, 60, 70, 80})}: pass
for x in {frozenset({frozenset({10, 20}), frozenset({30, 40})}),
          frozenset({frozenset({50, 60}), frozenset({70, 80})})}: pass
v1, v2, v3 = {0, 1, 2}
v1, *v2, v3 = {0, 1, 2, 3, 4, 5}
for v1, v2, v3 in {frozenset({0, 1, 2}), frozenset({3, 4, 5})}: pass
for v1, *v2, v3 in {frozenset({0, 1, 2, 3, 4, 5}),
                    frozenset({6, 7, 8, 9, 10, 11})}: pass
print(*{0, 1}, 2, *{3, 4, *{5}})
print({*{0, 1}, 2, *{3, 4, *{5}}})
A = {x**2 for x in {0, 1, 2, 3, 4, 5, 6, 7}}
A = {frozenset(y**2 for y in x) for x in {frozenset({0, 1, 2, 3}),
                                          frozenset({4, 5, 6, 7})}}
A = {frozenset(frozenset(z**2 for z in y) for y in x) for x in
        {frozenset({frozenset({0, 1}), frozenset({2, 3})}),
         frozenset({frozenset({4, 5}), frozenset({6, 7})})}}
# No error

A = {10, 20, [30, 40], 50, 60}
A = {10, 20, {30, 40}, 50, 60}
A = {10, 20, {30:40, 50:60}, 70, 80}
A = {bytearray(b'Hello')}
A = {10, 20, 30, 40, 50} * 3
A = set(range(1000000000))
# Error
Enter fullscreen mode Exit fullscreen mode

A set is the unordered collection of zero or more elements whose type is set as shown below:

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

print(A)
# {50, 20, 40, 10, 30}

print(type(A))
# <class 'set'>
Enter fullscreen mode Exit fullscreen mode
A = set() # Empty 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, 20, 30, 10, 20, 30}

print(A)
# {10, 20, 30}
Enter fullscreen mode Exit fullscreen mode
A = {0, 0.0, 0.0+0.0j, False}

print(A)
# {0}
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 the hashable types of elements 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

A set cannot have the unhashable types of elements as shown below:

A = {10, 20, [30, 40], 50, 60}       # set(list)
# TypeError: unhashable type: 'list'

A = {10, 20, {30, 40}, 50, 60}       # set(set)
# TypeError: unhashable type: 'set'

A = {10, 20, {30:40, 50:60}, 70, 80} # set(dict)
# TypeError: unhashable type: 'dict'

A = {bytearray(b'Hello')}            # set(bytearray)
# TypeError: unhashable type: 'bytearray'
Enter fullscreen mode Exit fullscreen mode

A set can be used with len() to get the length as shown below:

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

print(len(A))
# 5
Enter fullscreen mode Exit fullscreen mode

An empty set is False as shown below:

print(bool(set()))         # Empty set
# False

print(bool({0}))           # set
print(bool({frozenset()})) # set(Empty frozenset)
# True
Enter fullscreen mode Exit fullscreen mode

A set can be checked if a specific element is in the set with in keyword as shown below:

A = {10, 20, frozenset({30, 40})}

print(20 in A)
# True

print({30, 40} in A)
# True

print(2 in A)
# False
Enter fullscreen mode Exit fullscreen mode

A set cannot be enlarged with * and a number as shown below:

A = {10, 20, 30, 40, 50} * 3
# TypeError: unsupported operand type(s) for *: 'set' and 'int'
Enter fullscreen mode Exit fullscreen mode

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

A = range(1000000000)

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

A set can be iterated with a for statement as shown below:

<1D set>:

for x in {10, 20, 30, 40, 50}:
    print(x)
# 40
# 10
# 50
# 20
# 30
Enter fullscreen mode Exit fullscreen mode

<2D set>:

for x in {frozenset({10, 20, 30, 40}), frozenset({50, 60, 70, 80})}:
    for y in x:
        print(y)
# 40
# 10
# 20
# 30
# 80
# 50
# 60
# 70
Enter fullscreen mode Exit fullscreen mode

<3D set>:

for x in {frozenset({frozenset({10, 20}), frozenset({30, 40})}),
          frozenset({frozenset({50, 60}), frozenset({70, 80})})}:
    for y in x:
        for z in y:
            print(z)
# 40
# 30
# 10
# 20
# 80
# 70
# 50
# 60
Enter fullscreen mode Exit fullscreen mode

Top comments (0)