Repository navigation
Expand file tree
/
Copy path077-Pop-Element-from-Set.py
More file actions
81 lines (66 loc) · 3 KB
/
Copy path077-Pop-Element-from-Set.py
File metadata and controls
81 lines (66 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
===============================================================================
File Name : 77-Pop-Element-from-Set.py
Description : Pop an Element from a Set in Python
Author : Shaik Mahaboob Basha
===============================================================================
Definition:
The pop() method is used to remove and return an arbitrary element from a set.
Since sets are unordered collections, the element removed is not guaranteed
to be the first or last element.
Syntax:
set_name.pop()
Example:
removed_element = numbers.pop()
"""
# -----------------------------------------------------------------------------
# Creating a set of integers.
# -----------------------------------------------------------------------------
numbers = {10, 20, 30, 40, 50}
# -----------------------------------------------------------------------------
# Displaying the original set.
# -----------------------------------------------------------------------------
print("Original Set :", numbers)
# Output:
# Original Set : {10, 20, 30, 40, 50}
print()
# -----------------------------------------------------------------------------
# Removing an arbitrary element from the set.
# -----------------------------------------------------------------------------
removed_element = numbers.pop()
# -----------------------------------------------------------------------------
# Displaying the removed element.
# -----------------------------------------------------------------------------
print("Removed Element :", removed_element)
# Output:
# Removed Element : (Any one element from the set)
# -----------------------------------------------------------------------------
# Displaying the updated set.
# -----------------------------------------------------------------------------
print("Set After pop() :", numbers)
# Output:
# Set After pop() : Remaining elements after removing one element
print()
# -----------------------------------------------------------------------------
# Removing another arbitrary element from the set.
# -----------------------------------------------------------------------------
removed_element = numbers.pop()
# -----------------------------------------------------------------------------
# Displaying the removed element.
# -----------------------------------------------------------------------------
print("Removed Element :", removed_element)
# Output:
# Removed Element : (Any one remaining element)
# -----------------------------------------------------------------------------
# Displaying the updated set.
# -----------------------------------------------------------------------------
print("Set After Second pop() :", numbers)
# Output:
# Set After Second pop() : Remaining elements after removing another element
print()
# -----------------------------------------------------------------------------
# Displaying the total number of elements in the set.
# -----------------------------------------------------------------------------
print("Total Elements :", len(numbers))
# Output:
# Total Elements : 3