BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python Set pop() method with examples

Last Updated: April 1, 2019 by Chaitanya Singh | Filed Under: Python Tutorial

The pop() method in Python removes a random element from a given set and returns the removed element.

Set pop() method Syntax

set.pop()

Parameter: This method doesn’t take any parameter.
Return Value: It returns the removed element of the Set.

If you call this method on an empty set then an error is raised.

Python Set pop() method Example

In the following example we have a Set X and we are calling pop() method on this set. The pop() method removes a random element from the set and returns the removed element.

# My Set
X ={"hello", 2, 10, 99, "hi"}

# Displaying Set before calling pop()
print("Original Set is:",X)

# Calling pop() method
print("Removed element:",X.pop())

# Displaying Set after calling pop()
print("Set after pop():", X)

Output:
Python pop() method example

Top Related Articles:

  1. Python Recursion
  2. Python Data Types
  3. Python Keywords and Identifiers with examples
  4. Python Constructors – default and parameterized
  5. Python bytes() Function (With Examples)

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Python Tutorial

Introduction

  • Python Tutorial
  • Python Introduction
  • Install Python
  • PyCharm IDE Installation
  • Python in PyCharm
  • Python Comments
  • Python Variables
  • Python Keywords & Identifiers
  • Python data types

Flow Control

  • Python If
  • Python if..else
  • Python if..elif..else
  • Python Nested If
  • Python for loop
  • Python while loop
  • Python break
  • Python continue
  • Python pass

Python Functions

  • Python Functions
  • Python Recursion

Python Datatypes

  • Python Numbers
  • Python List
  • Python Strings
  • Python Tuple
  • Python Dictionary
  • Python Set

Python OOPs

  • Python OOP
  • Python Class & Object
  • Python Constructors

Python Examples

  • Python Programs

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap