Home » Python

Python Set pop() Method with Example

Python Set pop() Method: Here, we are going to learn how to remove an element from the set in Python?
Submitted by IncludeHelp, on November 30, 2019

Set pop() Method

pop() method is used to remove a random element from the set, the method is called with this set and remove a random element from the set.

Syntax:

    set_name.pop()

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is the type of the element, it returns the removed/popped element.

Example:

# Python Set pop() Method with Example

# declaring the sets
cars = {"Porsche", "Audi", "Lexus", "Mazda", "Lincoln"}
nums = {100, 200, 300, 400, 500}

# printing the sets before removing
print("Before the calling pop() method...")
print("cars: ", cars)
print("nums: ", nums)

# Removing/popping the elements from the sets
x = cars.pop()
print(x, "is removed from cars")
x = cars.pop()
print(x, "is removed from cars")
x = cars.pop()
print(x, "is removed from cars")

x = nums.pop()
print(x, "is removed from nums")
x = nums.pop()
print(x, "is removed from nums")
x = nums.pop()
print(x, "is removed from nums")

# printing the sets after removing
print("After the calling pop() method...")
print("cars: ", cars)
print("nums: ", nums)

Output

Before the calling pop() method...
cars:  {'Audi', 'Porsche', 'Lexus', 'Mazda', 'Lincoln'}
nums:  {100, 200, 300, 400, 500}
Audi is removed from cars
Porsche is removed from cars
Lexus is removed from cars
100 is removed from nums
200 is removed from nums
300 is removed from nums
After the calling pop() method...
cars:  {'Mazda', 'Lincoln'}
nums:  {400, 500}






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.