Pop
In [1]:
import pprint
In [2]:
# Removing a Patient’s Record
patients = {
"patient_001": {"name": "John Doe", "age": 30, "condition": "Hypertension"},
"patient_002": {"name": "Jane Smith", "age": 25, "condition": "Asthma"},
"patient_003": {"name": "Emily Davis", "age": 40, "condition": "Diabetes"}
}
removed_patient = patients.pop("patient_002")
print("Removed Patient:")
pprint.pp(removed_patient)
print("Updated Patients Dictionary:")
pprint.pp(patients)
Removed Patient:
{'name': 'Jane Smith', 'age': 25, 'condition': 'Asthma'}
Updated Patients Dictionary:
{'patient_001': {'name': 'John Doe', 'age': 30, 'condition': 'Hypertension'},
'patient_003': {'name': 'Emily Davis', 'age': 40, 'condition': 'Diabetes'}}
In [3]:
# Handling Missing Data
health_data = {
"heart_rate": 72,
"blood_pressure": "120/80",
"cholesterol": 190
}
removed_value = health_data.pop("blood_sugar", "Not Available")
print("Removed Value:")
pprint.pp(removed_value)
print("Updated Health Data Dictionary:")
pprint.pp(health_data)
Removed Value:
'Not Available'
Updated Health Data Dictionary:
{'heart_rate': 72, 'blood_pressure': '120/80', 'cholesterol': 190}
In [4]:
#Removing Multiple Entries
test_results = {
"test_001": {"name": "CBC", "result": "Normal"},
"test_002": {"name": "Lipid Panel", "result": "High Cholesterol"},
"test_003": {"name": "Blood Glucose", "result": "Normal"}
}
removed_test_001 = test_results.pop("test_001")
removed_test_002 = test_results.pop("test_002")
print("Removed Test 001:", removed_test_001)
print("Removed Test 002:", removed_test_002)
print("Updated Test Results Dictionary:", test_results)
Removed Test 001: {'name': 'CBC', 'result': 'Normal'}
Removed Test 002: {'name': 'Lipid Panel', 'result': 'High Cholesterol'}
Updated Test Results Dictionary: {'test_003': {'name': 'Blood Glucose', 'result': 'Normal'}}
In [5]:
# Using popitem() to remove the last inserted item
patient_data = {
"patient_001": {"name": "John Doe", "age": 30, "condition": "Flu"},
"patient_002": {"name": "Jane Smith", "age": 25, "condition": "Cold"},
"patient_003": {"name": "Emily Davis", "age": 40, "condition": "Asthma"},
"patient_004": {"name": "Michael Brown", "age": 50, "condition": "Diabetes"}
}
last_patient = patient_data.popitem()
print("Removed patient data:")
pprint.pp(last_patient)
print("Updated patient data dictionary:")
pprint.pp(patient_data)
Removed patient data:
('patient_004', {'name': 'Michael Brown', 'age': 50, 'condition': 'Diabetes'})
Updated patient data dictionary:
{'patient_001': {'name': 'John Doe', 'age': 30, 'condition': 'Flu'},
'patient_002': {'name': 'Jane Smith', 'age': 25, 'condition': 'Cold'},
'patient_003': {'name': 'Emily Davis', 'age': 40, 'condition': 'Asthma'}}
