Repository navigation
Expand file tree
/
Copy path085-Remove-Last-Key-Using-Popitem.py
More file actions
90 lines (75 loc) · 3.23 KB
/
Copy path085-Remove-Last-Key-Using-Popitem.py
File metadata and controls
90 lines (75 loc) · 3.23 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 : 85-Remove-Last-Key-Using-Popitem.py
Description : Remove the Last Key Using popitem() in Python Dictionary
Author : Shaik Mahaboob Basha
===============================================================================
Definition:
The popitem() method is used to remove and return the last inserted key-value
pair from a dictionary.
Syntax:
dictionary_name.popitem()
Example:
student.popitem()
"""
# -----------------------------------------------------------------------------
# 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 last inserted key-value pair.
# -----------------------------------------------------------------------------
removed_item = student.popitem()
# -----------------------------------------------------------------------------
# Displaying the removed key-value pair.
# -----------------------------------------------------------------------------
print("Removed Key-Value Pair :", removed_item)
# Output:
# Removed Key-Value Pair : ('city', 'Hyderabad')
# -----------------------------------------------------------------------------
# Displaying the updated dictionary.
# -----------------------------------------------------------------------------
print("Dictionary After First popitem() :", student)
# Output:
# Dictionary After First popitem() :
# {'id': 101, 'name': 'Rahul', 'course': 'Python', 'marks': 95}
print()
# -----------------------------------------------------------------------------
# Removing another last inserted key-value pair.
# -----------------------------------------------------------------------------
removed_item = student.popitem()
# -----------------------------------------------------------------------------
# Displaying the removed key-value pair.
# -----------------------------------------------------------------------------
print("Removed Key-Value Pair :", removed_item)
# Output:
# Removed Key-Value Pair : ('marks', 95)
# -----------------------------------------------------------------------------
# Displaying the updated dictionary.
# -----------------------------------------------------------------------------
print("Dictionary After Second popitem() :", student)
# Output:
# Dictionary After Second popitem() :
# {'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