Unit III · Basics of Python Programming for Pharmaceutical Sciences (BP101T) · As per PCI B.Pharmacy Syllabus, NEP 2020
Every pharmacy program that processes drug names, patient records, or lab values eventually needs a way to hold more than one value under a single name. Python gives you three built-in tools for exactly this job: lists, tuples, and dictionaries. This post explains what each one is, how it behaves internally, and why a pharmacy programmer would reach for one over another — with runnable code built around drug and patient examples throughout.

What Is a Data Structure?
A data structure is simply a systematic way of arranging and storing information in a computer’s memory so that it can be retrieved, updated, and processed efficiently. Without data structures, every piece of data would need its own separate variable — unworkable the moment you have a list of twenty drug names or a hundred patient records.
Linear vs Non-Linear Data Structures
Data structures are broadly split into two families. In a linear structure, elements sit one after another in sequence — arrays, linked lists, stacks (Last In, First Out), and queues (First In, First Out) all fall here. In a non-linear structure, elements connect in more complex ways — trees model parent-child hierarchies, and graphs connect elements through vertices and edges. Python’s lists, tuples, and dictionaries are the linear/associative structures you will use constantly in pharmacy computing; trees and graphs appear later in more advanced data science work.
Regardless of which structure you use, the same five operations recur everywhere: insertion (adding an element), deletion (removing one), traversal (visiting each element in turn), searching (locating a value), and sorting (arranging elements in order). Learning lists, tuples, and dictionaries really means learning how each of these five operations is performed on each structure.
Lists: Python’s Flexible, Ordered Container
A list is Python’s built-in structure for holding several values together under one variable name. Because a single list can carry many items, it is the most natural way to group related pharmacy data — a set of drug names, a batch of test results, or a roster of patients.
Key Characteristics of Lists
- Ordered — every element occupies a definite position, and indexing starts at 0.
- Mutable — once created, a list’s contents can be changed, added to, or removed.
- Dynamic size — a list grows or shrinks as items are added or removed; it is not fixed in length.
- Allows duplicates — the same value can appear more than once.
- Heterogeneous — a single list can mix integers, strings, and floats together.
Internally, Python implements a list as a dynamic array. Python quietly reserves a little extra memory beyond what is currently used, so that when you append a new element, the list can usually grow in place without having to be rebuilt from scratch every single time. This is why append() is fast on average, even though a list can, in theory, resize.
Core Operations on Lists
The five general operations map onto specific list methods: traversal is done with a loop, insertion uses append() (add to the end) or insert() (add at a chosen position), deletion uses remove() (delete by value), pop() (delete by position, and return it), or the del statement, searching uses the in keyword, and sorting uses the sort() method.
medicines = ["Paracetamol", "Ibuprofen", "Amoxicillin"]
# Insertion: add a new medicine to the end
medicines.append("Aspirin")
# Update: modify an existing entry by index
medicines[1] = "Diclofenac"
print(medicines)
# ['Paracetamol', 'Diclofenac', 'Amoxicillin', 'Aspirin']
Advantages and Disadvantages of Lists
Lists are flexible, easy to use, and come with a rich set of built-in methods, which makes them a natural fit whenever the amount of data is not fixed in advance. The trade-off is performance: because a list must remain resizable and mutable, Python does extra bookkeeping behind the scenes, making lists slightly slower and more memory-hungry than the fixed, unchanging tuple.
Tuples: Fixed, Protected Sequences
A tuple is Python’s other built-in sequence type. It looks like a list on the surface — an ordered collection accessed by index — but once a tuple is created, its contents can never be changed.
Characteristics of Tuples
- Ordered — elements keep a fixed sequence, exactly like a list.
- Immutable — elements cannot be added, removed, or changed after creation.
- Allows duplicates — the same value may appear more than once.
- Heterogeneous — different data types can be mixed within one tuple.
Because a tuple can never be resized once built, Python is free to store it in a fixed block of memory. This fixed storage is exactly what makes tuples faster to work with, and more memory-efficient, than the equivalent list.
drug_info = ("Paracetamol", 500, "Tablet")
# Access by index
print(drug_info[0]) # Paracetamol (drug name)
# Cannot modify - this line would raise a TypeError:
# drug_info[1] = 650
Because tuples cannot be modified, they support only a small set of methods — mainly count() (how many times a value appears) and index() (the position of a value) — alongside the general sequence operations of indexed access and slicing.
Tuples earn their keep in three situations: they execute faster than lists because Python does not need to manage possible resizing; they protect data from accidental edits, which matters when a fixed dosage value or a reference constant must never change mid-program; and, because they are immutable, they can be used as dictionary keys — something a list, being mutable, is never allowed to do.
Dictionaries: Looking Up Data by Key, Not Position
A dictionary stores information as key-value pairs, letting you retrieve a value directly through its associated key instead of hunting for it by numeric position. This is precisely the shape of a real patient record: you don’t want to remember that “disease” happens to be the third field — you want to just ask for patient["disease"].
Characteristics of Dictionaries
- Insertion-ordered — conceptually unordered by key, but from Python 3.7 onward a dictionary remembers and preserves the order in which items were inserted.
- Mutable — key-value pairs can be changed after the dictionary is created.
- Unique, immutable keys — every key must be distinct, and each key itself must be of an immutable type (a string, number, or tuple — never a list).
- Flexible values — values may repeat, and a value can be of any data type.
Internally, dictionaries are built on hash tables. Hashing is what allows Python to jump almost directly to the value associated with a given key, without scanning through every entry — this is why dictionary lookup runs, on average, in constant time, O(1).
Working With a Patient Record Dictionary
patient = {
"name": "Rahul",
"age": 30,
"disease": "Fever",
"medicine": "Paracetamol"
}
# Access a value by key
print(patient["disease"]) # Fever
# Update an existing value
patient["medicine"] = "Ibuprofen"
# Add a brand-new key-value pair
patient["city"] = "Mumbai"
print(patient)
# {'name': 'Rahul', 'age': 30, 'disease': 'Fever',
# 'medicine': 'Ibuprofen', 'city': 'Mumbai'}
A value is retrieved with dict[key], added or updated by assigning to dict[key] = value, and removed with del dict[key]. Three methods are used constantly when working with stored data: keys() returns all the keys, values() returns all the values, and items() returns the key-value pairs together, which is especially useful for looping through an entire record.
Choosing the Right Structure
The table below summarises when each structure is the right tool for a pharmacy programming task.
| Feature | List | Tuple | Dictionary |
|---|---|---|---|
| Mutability | Mutable | Immutable | Mutable |
| Access method | By index (position) | By index (position) | By key |
| Typical lookup speed | O(n) | O(n) | O(1) average |
| Can be a dictionary key? | No | Yes | — |
| Best pharmacy use case | Drug stock list, growing dataset | Fixed dosage values, constants | Patient record, labelled data |
Frequently Asked Questions
Why would I ever use a tuple instead of a list if a list can do everything a tuple can?
A tuple’s immutability is a feature, not a limitation. If you have a fixed dosage triplet like (500, 200, 100) that should never accidentally change during program execution, a tuple guarantees that. Tuples are also faster and more memory-efficient, and only a tuple (never a list) can be used as a dictionary key.
Can a dictionary key be a list?
No. Dictionary keys must be of an immutable type, and a list is mutable, so Python will raise a TypeError: unhashable type: 'list' if you try. Strings, numbers, and tuples are all valid keys.
What happens if I try to access a list index that doesn’t exist?
Python raises an IndexError. For a dictionary, accessing a missing key with square brackets raises a KeyError instead — using dict.get(key) is a safer alternative that returns None rather than crashing the program.
Summary
Lists, tuples, and dictionaries are Python’s three foundational ways of grouping data, and each is suited to a different pharmacy scenario. Lists are ordered and mutable, ideal for a drug stock list that keeps changing. Tuples are ordered and immutable, ideal for fixed values like a dosage constant that must never be altered. Dictionaries map keys to values with fast, hash-table-based lookup, making them the natural choice for structured records such as a patient’s prescription. Mastering when to use each one is the first real step toward writing pharmacy software that manages data cleanly and efficiently.
References
- Pharmacy Council of India (PCI), B.Pharm Regulations, NEP 2020 — BP101T Syllabus, Basics of Python Programming for Pharmaceutical Sciences
- Python Software Foundation, “Data Structures” — official Python documentation, docs.python.org
- Python Software Foundation, “Built-in Types: list, tuple, dict” — official Python documentation, docs.python.org
