Lesson · 40 min · Free
Python Dictionaries Masterclass
Python Dictionaries Masterclass body { font-family: sans-serif; line-height: 1.6; margin: 20px; } h1, h2 { color: #2c3e50; } pre { background-color: #ecf0f1; padding: 10px; border-radius: 5px; overflow-x: auto; } code {
Python Dictionaries Masterclass
In pharmaceutical research, data management and analysis are paramount. Often, we encounter data that isn't best represented by simple ordered lists or numerical arrays. Instead, we need to associate specific pieces of information with descriptive labels, much like a patient's medical chart associates symptoms with diagnoses, or a drug's profile associates a compound ID with its half-life and mechanism of action. This is precisely where Python dictionaries become an indispensable tool. A Python dictionary is an unordered collection of data values, used to store data values like a map, which, unlike other data types that hold only a single value as an element, holds key:value pairs. Each key must be unique and immutable (e.g., strings, numbers, tuples), while values can be of any data type and can be duplicated. This structure mirrors many real-world datasets in pharmacy and biotech, making dictionaries incredibly powerful for organizing and accessing complex information.
Creating and Accessing Dictionaries for Pharmaceutical Data
Dictionaries are defined by enclosing a comma-separated list of key:value pairs within curly braces {} . To access a value, you refer to its corresponding key using square brackets [] . Let's consider a practical example: storing information about a new drug candidate in a research pipeline. # Creating a dictionary for a drug candidate drug_candidate = { "compound_id": "DRG-001A", "target_protein": "EGFR", "mechanism_of_action": "Tyrosine Kinase Inhibitor", "phase": "Pre-clinical", "in_vitro_potency_nM": 15, "side_effects": ["Nausea", "Headache"], "researcher_lead": "Dr. Anya Sharma" } print(f"Compound ID: {drug_candidate['compound_id']}") print(f"Target Protein: {drug_candidate['target_protein']}") print(f"Current Phase: {drug_candidate['phase']}") # Accessing a list within the dictionary print(f"Known Side Effects: {', '.join(drug_candidate['side_effects'])}") # Attempting to access a non-existent key will raise a KeyError # print(drug_candidate['toxicity_profile']) # This would cause an error You can also add new key-value pairs or modify existing ones simply by assigning a value to a key. This dynamic nature is extremely useful when updating research data or expanding a drug's profile as new information becomes available. # Adding new information to the drug candidate dictionary drug_candidate["bioavailability_percent"] = 78.5 drug_candidate["phase"] = "Phase I" # Updating the phase print("\nUpdated Drug Candidate Information:") for key, value in drug_candidate.items(): print(f"{key.replace('_', ' ').title()}: {value}") # Removing a key-value pair del drug_candidate["side_effects"] print("\nAfter removing side effects:") print(drug_candidate) The .items() method used in the example above is particularly useful for iterating through all key-value pairs in a dictionary, allowing for comprehensive data review or processing. Other useful methods include .keys() to get a list of all keys and .values() to get a list of all values.
Key Takeaways:
Dictionaries store data as unordered key:value pairs. Keys must be unique and immutable (e.g., strings, numbers, tuples). Values can be of any data type and can be duplicated. They are ideal for representing structured data where each piece of information has a descriptive label. You can easily add, modify, and delete key-value pairs. Methods like .keys() , .values() , and .items() provide efficient ways to interact with dictionary contents.
Practice Exercise:
Imagine you are managing a batch of patient samples for a clinical trial. Each sample has a unique ID, patient age, patient gender, and the concentration of a specific biomarker. Create a Python dictionary called sample_data to store this information for at least three different patient samples. Then, write code to: 1) Add a new key-value pair to one of the samples indicating the "date_collected", and 2) Print the biomarker concentration for a specific sample ID. (Hint: You might consider making the sample ID the key for an outer dictionary, and then each value could be another dictionary containing the patient details).
Watch the full lesson — free
This topic is part of Python for Pharmaceutical Research, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →