![]() |
Given a set, the task is to write a Python program to delete the last element from the set. Example:
Note: Sets are unordered which means any element in the set has no fixed order and it can change therefore we cannot delete any particular element with respect to its position. Remove the last element from the set Using discard() methoddiscard() method can also remove the element from the set, by passing the element into this function. Python3
Output
{1, 2, 3} {'For'} Remove the last element from set Using remove()remove() can be also used in the same ways as we see in method 1. Python3
Output
{1, 2, 3} {'For'} Note: If the element is not present in the set then the remove method raises an error so if you’re not sure that the element is present or not then use the discard method. Remove the last element from the set Using pop()Python3
Output
{2, 3, 4} {'For'} Note: pop() method deletes random elements in the set and it raises a error if the set is empty. Remove the last element from the set using listIn this method we first convert a set into a list then we remove the last element in that list using slicing. And in last we convert that list back to set. Python3
Output
{1, 2, 3} {'Geeks'} |
Reffered: https://www.geeksforgeeks.org
Python |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |