April 29, 2026

Indexing & Slicing in Python (Extracting Patient Data & Dose Trends)

Indexing & Slicing in Python for Pharmacy Students (Extracting Patient Data & Dose Trends)

Indexing and slicing are essential techniques used to access and extract specific data from lists, tuples, and strings. In pharmaceutical applications, they are widely used for analyzing patient data, extracting drug information, and studying dose trends.


๐Ÿ”ท What is Indexing?

Indexing is used to access individual elements in a data structure using their position.

doses = [500, 650, 400]
print(doses[0])  # Output: 500

๐Ÿ‘‰ Index starts from 0 in Python.


๐Ÿ”ท Negative Indexing

Negative indexing allows accessing elements from the end.

print(doses[-1])  # Output: 400

๐Ÿ”ท What is Slicing?

Slicing extracts a portion of data using start and end index.

doses = [500, 650, 400, 550]
print(doses[1:3])  # Output: [650, 400]

๐Ÿ”ท Slicing with Step

print(doses[0:4:2])  # Output: [500, 400]

Step value helps in skipping elements.


๐Ÿ”ท Indexing & Slicing in Strings

drug = "Paracetamol"

print(drug[0])     # P
print(drug[0:4])   # Para

๐Ÿ’Š Patient Dose Trend Analysis

doses = [500, 650, 400, 550, 600]

# First 3 doses
print(doses[0:3])

# Last 2 doses
print(doses[-2:])

Useful for analyzing dosage trends over time.


๐Ÿ’Š Extracting Patient Data

patients = ["Alice", "Bob", "Charlie", "David"]

# First patient
print(patients[0])

# Last patient
print(patients[-1])

๐Ÿ”ท Accessing Dictionary Data

patient = {
    "name": "John",
    "age": 45,
    "drug": "Aspirin"
}

print(patient["name"])

๐Ÿง  Memory Tricks

  • Index = Position
  • Slice = Range
  • -1 = Last element

๐Ÿงช Practice Exercise

Create a list of drug doses and:

  • Print first 2 doses
  • Print last 2 doses
  • Print alternate doses

๐Ÿงช Mini Project

Analyze patient doses:

doses = [500, 650, 400, 550, 600]

average_first_half = doses[0:3]
print("First half doses:", average_first_half)

๐Ÿ“ MCQs

  1. Index starts from:
    a) 1
    b) 0
    c) -1
    d) None
    Answer: b

  2. Which index gives last element?
    a) 0
    b) 1
    c) -1
    d) 2
    Answer: c

  3. Slicing syntax:
    a) [start:end]
    b) (start:end)
    c) {start:end}
    d)
    Answer: a

โ“ FAQs

Why is slicing important in pharmacy?

It helps extract specific patient or drug data for analysis.

What is negative indexing?

Accessing elements from the end using negative numbers.


๐Ÿ“ฅ Download Data Extraction Practice Sets

Includes patient datasets and dose analysis problems.


โžก Next Topic: NumPy Arrays in Python โ†’

Recommemded readings

  1. Lists, Tuples & Dictionaries (with pharma data examples)
  2. Indexing, Slicing & Operations (Extracting patient/drug data) : Current page
  3. NumPy Arrays (Dose calculations, concentration arrays)
  4. CSV File Handling (Reading ADR datasets, writing reports)
  5. Understanding Healthcare Datasets (Structure, columns, patient data interpretation)
  6. Data Access & Manipulation (Filter, select, basic operations)

Question Bank Unit 3: Data Structures & File Handling

For more details: Basics of Python Programming for Pharmaceutical Sciences