YouTip LogoYouTip

Ref Set Pop

# Python3.x Python Set pop() Method [![Image 3: Python3 Lists](#) Python Sets](#) * * * ## Description The pop() method is used to remove and return a random element from a set. If the set is empty, it raises a KeyError exception. ## Syntax The syntax for the pop() method is: set.pop() ## Parameters * None ## Return Value Returns the removed element. ## Example Randomly remove an element: ## Example 1 fruits = {"apple", "banana", "cherry"} fruits.pop()print(fruits) The output result is: {'apple', 'banana'} Output the return value: ## Example 1 fruits = {"apple", "banana", "cherry"} x = fruits.pop()print(x) The output result is: banana ### Notes * The elements in a set are unordered, so each time the `pop()` method is called, the element removed is random. * If you need to remove a specific element, you should use the `remove()` or `discard()` method instead of `pop()`. [![Image 4: Python3 Lists](#) Python Sets](#)
← Ref Set Symmetric_DifferenceRef Set Issubset β†’