TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python Set pop() method with Examples

Python Set pop() method with Examples on python set add() method, python set discard() method, python set pop() method, python set remove() method, python tutorial, introduction, applications etc.

<< Back to PYTHON

Python Set pop() Method

Python pop() method pops an element from the set. It does not take any argument but returns the popped element. It raises an error if the element is not present in the set. See the examples and signature of the method is given below.

Signature

pop()

Parameters

No parameter.

Return

It returns deleted element or throws an error if the set is empty.

Let's see some examples of pop() method to understand it's functionality.

Python Set pop() Method Example 1

A simple example to use pop() method which removes an element and modifies the set.

# Python set pop() Method
# Creating a set
set = {1,2,3,4,5}
# Displaying elements
print(set) 
# Calling function
el = set.pop()
print("Element popped:",el)
print("Remaining elements: ",set)
el = set.pop()
print("Element popped:",el)
print("Remaining elements: ",set)
el = set.pop()
print("Element popped:",el)
print("Remaining elements: ",set)
el = set.pop()
print("Element popped:",el)
print("Remaining elements: ",set)

Output:

{1, 2, 3, 4, 5}
Element popped: 1
Remaining elements:  {2, 3, 4, 5}
Element popped: 2
Remaining elements:  {3, 4, 5}
Element popped: 3
Remaining elements:  {4, 5}
Element popped: 4
Remaining elements:  {5}

Python Set pop() Method Example 2

If the set is empty, it throws an error KeyError to the caller function. See the example.

# Python set pop() Method
# Creating a set
set = {1,2}
# Displaying elements
print(set) 
# Calling function
el = set.pop()
print("Element popped:",el)
print("Remaining elements: ",set)
el = set.pop()
print("Element popped:",el)
print("Remaining elements: ",set)
el = set.pop()
print("Element popped:",el)
print("Remaining elements: ",set)

Output:

{1, 2}
Element popped: 1
Remaining elements:  {2}
Element popped: 2
Remaining elements:  set()
Traceback (most recent call last):
  File "main.py", line 13, in 
    el = set.pop()
KeyError: 'pop from an empty set'

Python Set pop() Method Example 3

This example contains adding and poping elements in sequence to describe the functioning.

# Python set pop() Method
# Creating a set
set = {1,2}
# Displaying elements
print(set) 
# Calling function
el = set.pop()
print("Element popped:",el)
print("Remaining elements: ",set)
set.add(4)
el = set.pop()
print("Element popped:",el)
print("Remaining elements: ",set)
set.add(5)
el = set.pop()
print("Element popped:",el)
print("Remaining elements: ",set)

Output:

{1, 2}
Element popped: 1
Remaining elements:  {2}
Element popped: 2
Remaining elements:  {4}
Element popped: 4
Remaining elements:  {5}

Next TopicPython Set




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf