.pop()
Anonymous contributor
Published Jun 29, 2024
Contribute to Docs
In Python, the .pop()
method randomly removes an element from a set and returns the element that was removed. If the set is empty, the method will raise a TypeError
since there are no elements to remove from the set.
Syntax
set.pop()
set
: The set from which an element will be removed.
Example
The following example demonstrates the usage of the .pop()
method:
set1 = {"Alpha", "Beta", "Gamma"}print(set1.pop())print(set1)
The output of .pop()
will vary each time it is executed since the method removes and returns a random element from the set. Here is one potential output of this example where Beta
is randomly selected to be removed from set1
:
Beta{'Gamma', 'Alpha'}
Codebyte Example
Here is a codebyte example to understand how the .pop()
method works:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours