Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs
    • SQL FAQ’s

Python set pop

by suresh

The Python set pop method is one of the set method used to remove a random from a set. It is because the Python set won’t store the items using indexes. You can assign that removed item to a new variable for further reference.

The syntax of this Python set pop method is 

set.pop()

Python set pop example

In this example, we declared a numeric set. Next, we used this set pop function to remove a random set item from the existing set. 

# Python set pop
numeric_set = {150, 250, 350, 450, 550}
 
print('The Original Set : ', numeric_set )
 
numeric_set.pop()

print('The Set after pop: ', numeric_set)

OUTPUT

Python set pop method 1

This set pop code is the same as the above example. However, this time we are assigning the removed set value to a new variable and printing the same.

TIP: Please refer to the Python set to understand Sets in Python.

numeric_set = {150, 250, 350, 450, 550}
 
print('The Original Set : ', numeric_set )
 
x = numeric_set.pop()
 
print('\npop Item         : ', x)

print('The Set after pop: ', numeric_set)

OUTPUT

Python set pop method 2

Python pop example 2

In this example, we are using the set pop method to remove all the existing set items one after the other.

numeric_set = {15, 25, 35, 45, 55}
print('The Original Set : ', numeric_set )
 
x = numeric_set.pop()
print('\npop Item         : ', x)
print('The Set after pop: ', numeric_set)
 
y = numeric_set.pop()
print('\npop Item         : ', y)
print('The Set after pop: ', numeric_set)
 
z = numeric_set.pop()
print('\npop Item         : ', z)
print('The Set after pop: ', numeric_set)
 
zz = numeric_set.pop()
print('\npop Item         : ', zz)
print('The Set after pop: ', numeric_set)

OUTPUT

Python set pop method is 3

This time, we are working with Python pop function on the string set.

fruits_set = {'Mango', 'Cherry', 'Apple', 'Kiwi'}
print('The Original Set : ', fruits_set)
 
x = fruits_set.pop()
print('pop Item         : ', x)
print('The Set after pop: ', fruits_set)
 
y = fruits_set.pop()
print('\npop Item         : ', y)
print('The Set after pop: ', fruits_set)

OUTPUT

Python set pop method 4

Here, we declared a mixed set. Next, we used the set pop method to remove a random item from a set.

mixed_set = {'Mango', 10, 'Cherry', 20, 'Apple', 30, 'Kiwi'}
print('The Original Set : ', mixed_set)
 
x = mixed_set.pop()
print('\npop Item         : ', x)
print('The Set after pop: ', mixed_set)
 
y = mixed_set.pop()
print('\npop Item         : ', y)
print('The Set after pop: ', mixed_set)

OUTPUT

Python set pop method 5

Placed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python Numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python Numpy concatenate
  • Python Numpy Array shape
  • Python Pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python Substring
  • Python Programming Examples
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy