In [1]:
# Slicing a List of Integers
patient_ages = [25, 34, 45, 52, 63, 72, 81, 90]
age_slice = patient_ages[2:6]
print(age_slice)
[45, 52, 63, 72]
In [2]:
# Slicing a List of Strings
patient_names = ["Alice", "Bob", "Charlie", "David", "Eve", "Frank"]
name_slice = patient_names[1:4]
print(name_slice)
[''''Bob'''', ''''Charlie'''', ''''David'''']
In [3]:
import pprint
#Slicing a List of Dictionaries
patient_records = [
{"name": "Alice", "age": 25, "condition": "Flu"},
{"name": "Bob", "age": 34, "condition": "Cold"},
{"name": "Charlie", "age": 45, "condition": "Asthma"},
{"name": "David", "age": 52, "condition": "Diabetes"},
{"name": "Eve", "age": 63, "condition": "Hypertension"}
]
record_slice = patient_records[1:4]
pprint.pp(record_slice)
[{''''name'''': ''''Bob'''', ''''age'''': 34, ''''condition'''': ''''Cold''''}, {''''name'''': ''''Charlie'''', ''''age'''': 45, ''''condition'''': ''''Asthma''''}, {''''name'''': ''''David'''', ''''age'''': 52, ''''condition'''': ''''Diabetes''''}]