Repository navigation
Expand file tree
/
Copy path086-Delete-Key-Using-Del.py
More file actions
86 lines (70 loc) · 2.85 KB
/
Copy path086-Delete-Key-Using-Del.py
File metadata and controls
86 lines (70 loc) · 2.85 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
"""
===============================================================================
File Name : 86-Delete-Key-Using-Del.py
Description : Delete a Key Using del in Python Dictionary
Author : Shaik Mahaboob Basha
===============================================================================
Definition:
The del keyword is used to delete a specified key-value pair from a dictionary.
If the specified key does not exist, a KeyError occurs.
Syntax:
del dictionary_name[key]
Example:
del student["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()
# -----------------------------------------------------------------------------
# Deleting the "marks" key from the dictionary.
# -----------------------------------------------------------------------------
del student["marks"]
# -----------------------------------------------------------------------------
# Displaying the updated dictionary.
# -----------------------------------------------------------------------------
print("After Deleting 'marks' :", student)
# Output:
# After Deleting 'marks' :
# {'id': 101, 'name': 'Rahul', 'course': 'Python', 'city': 'Hyderabad'}
print()
# -----------------------------------------------------------------------------
# Deleting the "city" key from the dictionary.
# -----------------------------------------------------------------------------
del student["city"]
# -----------------------------------------------------------------------------
# Displaying the updated dictionary.
# -----------------------------------------------------------------------------
print("After Deleting 'city' :", student)
# Output:
# After Deleting '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
print()
# -----------------------------------------------------------------------------
# Attempting to delete a key that does not exist.
# -----------------------------------------------------------------------------
del student["age"]
# Output:
# KeyError: 'age'