Lesson · 40 min · Free
Python Lists Mastery
Python Lists Mastery body { font-family: sans-serif; line-height: 1.6; margin: 20px; } h1, h2 { color: #2c3e50; } pre, code { background-color: #ecf0f1; padding: 10px; border-radius: 5px; overflow-x: auto; } ul { list-st
Python Lists Mastery
Welcome to this module on Python Lists! In the realm of scientific computing, especially within pharmacy and biotechnology, the ability to store and manipulate collections of data is paramount. Whether you're managing patient demographics, experimental results, chemical compound properties, or gene sequences, Python's list data structure provides a flexible and powerful tool for organizing this information. Understanding lists is a fundamental step towards more complex data analysis and algorithm development. At its core, a Python list is an ordered, mutable (changeable) collection of items. This means you can store various types of data within a single list – numbers, strings, even other lists – and you can modify its contents after it has been created. This flexibility makes lists incredibly versatile for many scientific applications, from maintaining a queue of samples to tracking the progression of a biological assay.
Creating and Accessing Lists
Creating a list in Python is straightforward; you simply enclose a comma-separated sequence of items within square brackets [] . Once created, individual elements can be accessed using their index. Python uses zero-based indexing, meaning the first element is at index 0 , the second at 1 , and so on. You can also use negative indexing to access elements from the end of the list, where -1 refers to the last element, -2 to the second to last, and so forth. # Example 1: Creating and Accessing Lists in a Biotech Context # List of patient IDs for a clinical trial patient_ids = ["P001", "P002", "P003", "P004", "P005"] print(f"All patient IDs: {patient_ids}") # Accessing the first patient ID first_patient = patient_ids[0] print(f"First patient ID: {first_patient}") # Output: P001 # Accessing the third patient ID third_patient = patient_ids[2] print(f"Third patient ID: {third_patient}") # Output: P003 # Accessing the last patient ID using negative indexing last_patient = patient_ids[-1] print(f"Last patient ID: {last_patient}") # Output: P005 # List of drug concentrations (in mM) for an experiment drug_concentrations = [0.1, 0.5, 1.0, 2.5, 5.0, 10.0] print(f"Drug concentrations: {drug_concentrations}") # Accessing a range of concentrations using slicing # Slicing extracts a portion of the list. The syntax is [start:end], # where 'start' is inclusive and 'end' is exclusive. intermediate_concentrations = drug_concentrations[2:5] print(f"Intermediate concentrations (index 2 to 4): {intermediate_concentrations}") # Output: [1.0, 2.5, 5.0] # Slicing from the beginning up to a certain index low_concentrations = drug_concentrations[:3] print(f"Low concentrations (first three): {low_concentrations}") # Output: [0.1, 0.5, 1.0] # Slicing from a certain index to the end high_concentrations = drug_concentrations[3:] print(f"High concentrations (from index 3 onwards): {high_concentrations}") # Output: [2.5, 5.0, 10.0] Beyond simple access, lists offer a rich set of methods for modification. You can add elements using .append() (adds to the end) or .insert() (adds at a specific index). Elements can be removed using .remove() (by value) or .pop() (by index, and returns the removed element). You can also directly reassign values at a specific index, demonstrating the mutable nature of lists. # Example 2: Modifying Lists for Lab Data Management # Initial list of compounds identified in a sample compounds = ["Glucose", "Lactose", "Sucrose"] print(f"Initial compounds: {compounds}") # Adding a new compound found compounds.append("Fructose") print(f"After appending Fructose: {compounds}") # Output: ['Glucose', 'Lactose', 'Sucrose', 'Fructose'] # Inserting a compound at a specific position (e.g., after Glucose) compounds.insert(1, "Maltose") print(f"After inserting Maltose at index 1: {compounds}") # Output: ['Glucose', 'Maltose', 'Lactose', 'Sucrose', 'Fructose'] # A lab technician discovers an error: Sucrose was misidentified. # We need to change it to Galactose. compounds[3] = "Galactose" print(f"After correcting Sucrose to Galactose: {compounds}") # Output: ['Glucose', 'Maltose', 'Lactose', 'Galactose', 'Fructose'] # Removing a compound that was later determined to be a contaminant compounds.remove("Maltose") print(f"After removing Maltose: {compounds}") # Output: ['Glucose', 'Lactose', 'Galactose', 'Fructose'] # Using .pop() to remove the last element and store it (e.g., for further processing) removed_compound = compounds.pop() print(f"Removed compound using pop(): {removed_compound}") # Output: Fructose print(f"List after pop(): {compounds}") # Output: ['Glucose', 'Lactose', 'Galactose'] # You can also pop by index removed_first_compound = compounds.pop(0) print(f"Removed first compound using pop(0): {removed_first_compound}") # Output: Glucose print(f"List after pop(0): {compounds}") # Output: ['Lactose', 'Galactose'] Lists are incredibly powerful for managing dynamic datasets. Their mutability and ordered nature make them ideal for scenarios where the sequence of data matters and where data points may be added, removed, or updated over time. This is a common requirement in experimental science, where data collection is often an iterative process.
Key Takeaways
Python lists are ordered, mutable collections of items. They are created using square brackets [] and items are separated by commas. Elements are accessed using zero-based indexing ( list[0] for the first item). Negative indexing ( list[-1] for the last item) allows access from the end. Slicing ( list[start:end] ) extracts sub-lists. Common modification methods include .append() , .insert() , .remove() , and .pop() . Individual elements can be changed by assigning a new value to their index.
Practice Exercise: Managing a Sample Inventory
You are managing an inventory of biological samples for a new experiment. Start with an empty list called sample_inventory . Add the following sample IDs: "Sample_A01", "Sample_B02", "Sample_C03". Then, due to a re-prioritization, remove "Sample_B02". Finally, insert a new sample ID, "Sample_D04", at the beginning of the list. Print the list after each modification to observe the changes.
Watch the full lesson — free
This topic is part of Python Programming - Basics, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →