⊗pyPmStEE 115 of 208 menu

Extracting an Element from a Set in Python

To extract a random element from a set, we need to apply the pop method. We don't pass anything to its parameter.

Let us have a set:

st = {'a', 'b', 'c'}

Let's extract a random element from it:

print(st.pop()) # 'b'

The element after extraction is removed from the set. Let's look at the original set:

print(st) # {'b', 'c'}

Given a set:

st = {1, 2, 3, 4, 5}

Reduce its length by one.

Given a set:

st = {'a1', 'b2', 'c3', 'd4'}

Perform a sequential extraction of two elements from it.

byenru