Repository navigation
Expand file tree
/
Copy path084-Remove-Key-Using-Pop.py
More file actions
90 lines (75 loc) · 3.16 KB
/
Copy path084-Remove-Key-Using-Pop.py
File metadata and controls
90 lines (75 loc) · 3.16 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
82
83
84
85
86
87
88
89
90
"""
===============================================================================
File Name : 84-Remove-Key-Using-Pop.py
Description : Remove a Key Using pop() in Python Dictionary
Author : Shaik Mahaboob Basha
===============================================================================
Definition:
The pop() method is used to remove a specified key from a dictionary and
returns the corresponding value.
Syntax:
dictionary_name.pop(key)
Example:
student.pop("marks")
"""
# -----------------------------------------------------------------------------
# Creating a student dictionary.
# -----------------------------------------------------------------------------
student = {
"id": 101,
"name": "Rahul",
"course": "Python",
"marks": 95,
"city": "Hyderabad"
}
# -----------------------------------------------------------------------------
# Displaying the original dictionary.
# -----------------------------------------------------------------------------
print("Original Dictionary :", student)
# Output:
# Original Dictionary :
# {'id': 101, 'name': 'Rahul', 'course': 'Python',
# 'marks': 95, 'city': 'Hyderabad'}
print()
# -----------------------------------------------------------------------------
# Removing the "marks" key using the pop() method.
# -----------------------------------------------------------------------------
removed_value = student.pop("marks")
# -----------------------------------------------------------------------------
# Displaying the removed value.
# -----------------------------------------------------------------------------
print("Removed Value :", removed_value)
# Output:
# Removed Value : 95
# -----------------------------------------------------------------------------
# Displaying the updated dictionary.
# -----------------------------------------------------------------------------
print("Dictionary After Removing 'marks' :", student)
# Output:
# Dictionary After Removing 'marks' :
# {'id': 101, 'name': 'Rahul', 'course': 'Python', 'city': 'Hyderabad'}
print()
# -----------------------------------------------------------------------------
# Removing another key using the pop() method.
# -----------------------------------------------------------------------------
removed_value = student.pop("city")
# -----------------------------------------------------------------------------
# Displaying the removed value.
# -----------------------------------------------------------------------------
print("Removed Value :", removed_value)
# Output:
# Removed Value : Hyderabad
# -----------------------------------------------------------------------------
# Displaying the updated dictionary.
# -----------------------------------------------------------------------------
print("Dictionary After Removing 'city' :", student)
# Output:
# Dictionary After Removing 'city' :
# {'id': 101, 'name': 'Rahul', 'course': 'Python'}
print()
# -----------------------------------------------------------------------------
# Displaying the total number of key-value pairs.
# -----------------------------------------------------------------------------
print("Total Key-Value Pairs :", len(student))
# Output:
# Total Key-Value Pairs : 3