Guide to Python Data Structures
Cancer Dataset
The most important thing about lists
In Python, lists are mutable, meaning their elements can be changed after the list is created, allowing for modification such as adding, removing, or updating items. This flexibility makes lists powerful for handling dynamic collections of data.
We will learn how to do things such as:
[1, 2, 3.2, 'a', 'b', 'c', [4, 'z']]
7
In Python, lists are objects like all other data types, and the class for lists is named ‘list’ with a lowercase ‘L’.
To transform another Python object into a list, you can use the list() function, which is essentially the constructor of the list class. This function accepts a single argument: an iterable. So, you can use it to turn any iterable, such as a range, set, or tuple, into a list of concrete values.
Python indices begin at 0. In addition, certain built-in python functions such as range will terminate at n-1 in the second argument.
[0, 1, 2, 3, 4]
[5, 6, 7, 8, 9]
To access an individual list element, you need to know its position. Since python starts counting at 0, the first element is in position 0, and the second element is in position 1. You can also access nested elements within a list, or access the list in reverse.
Since lists are mutable objects, we can directly change their elements.
[1, 2, 3]
['hello', 2, 3]
When calling append on a list, we append an object to the end of the list:
[1, 2, 3.2, 'a', 'b', 'c', [4, 'z']]
[1, 2, 3.2, 'a', 'b', 'c', [4, 'z'], 5]
We can combine lists with the “+” operator. This keeps the original lists intact
[1, 2, 3, 'a', 'b', 'c']
The .pop() method removes and returns the last item by default unless you give it an index argument. If you’re familiar with stacks, this method as well as .append() can be used to create one!
3 2
del removes an item without returning anything. In fact, you can delete any object, including the entire list, using del:
[2, 3]
The .remove() method deletes a specific value from the list. This method will remove the first occurrence of the given object in a list.
[2, 3]
The difference between a list and a set:
[1, 2, 3, 1, 2]
{1, 2, 3}
[1, 2, 3]
There are two ways to sort a list in Python
.sort() modifies the original list itself. Nothing is returned.
.sorted() returns a new list, which is a sorted version of the original list.
.reverse=True: Use this parameter to sort the list in reverse order.
[1, 2, 3, 5, 6, 19]
[19, 6, 5, 3, 2, 1]
['a', 'b', 'e', 'z']
['z', 'e', 'b', 'a']
Can't sort a list of mixed elements
List comprehension offers a shorter syntax when you want to create a new list based on the values of an existing list (or other object)
['a', 'b', 'c']
['JOE', 'SARAH', 'EMILY']
['a', 't']
['a', 'b', 'c']
['JOE', 'SARAH', 'EMILY']
['a', 't']
A tuple is similar to a list, but with one key difference. Tuples are immutable. This means that once you create a tuple, you cannot modify its elements.
Tuples are useful for storing data that should not be changed after creation, such as coordinates, days of the week, or fixed pairs.
Just like lists, tuples are objects, and the class for tuples is tuple.
To transform another Python object into a tuple, you can use the tuple() constructor. It accepts a single iterable, such as a list, range, or string.
Tuples are immutable, so you can’t modify their elements. Attempting to change a tuple will result in an error.
Tuples are immutable and cannot be changed!
Functions can return multiple values as a tuple. This is useful for returning multiple results in a single function call.
# Function that returns multiple values as a tuple
def min_max(nums):
return min(nums), max(nums) # Returns a tuple of (min, max)
# Calling the function and unpacking the tuple
numbers = [3, 7, 1, 5]
our_tuple = min_max(numbers)
min_val, max_val = min_max(numbers) #Unpacking in the function call
print(our_tuple)
print("Min:", min_val)
print("Max:", max_val)
(1, 7)
Min: 1
Max: 7
You can use single or double quotes to define a string (but keep it consistent!)
Hello, World!
You can find the length of a string using the len() funtion, just like with lists.
13
Strings are indexed like lists, with the first character having index 0. You can access individual characters using their index.
H ! Hello
Strings are immutable!
Unlike lists, strings cannot be changed after creation. If you try to change an individual character, you’ll get an error.
Strings are immutable!
You can concatenate (combine) strings using the + operator:
Hello, Patrick!
Python provides many built-in methods for manipulating strings. Some common ones are:
upper() and lower() These methods convert a string to uppercase or lowercase.
HELLO, WORLD!
hello, world!
strip() This method removes any leading or trailing whitespace from the string.
Hello, World!
replace() You can replace parts of a string with another string.
Hello, Patrick!
The split() method divides a string into a list of substrings based on a delimiter (default is whitespace).
['Hello,', 'World!']
['Hello', 'World!']
You can insert variables directly into strings using f-strings.
My name is Patrick and I am 30 years old.
['e', 'o', 'o']
We will slice a string using different combinations of start, end, and step to extract different parts of the string.
Original string: Python is awesome!
Substring 1 (0:6): Python
Substring 2 (7:): is awesome!
Substring 3 (every second character): Pto saeoe
Substring 4 (0:6:2): Pto
Substring 5 (11:6:-1): wa si
Accessing dictionary values
Alice
25
Not specified
Dictionaries are mutable, so you can add new items or update existing ones using assignment.
{'name': 'Alice', 'age': 26, 'city': 'New York', 'job': 'Engineer'}
Python dictionaries have several useful methods for managing data:
dict_keys(['name', 'age', 'city', 'job'])
dict_values(['Alice', 26, 'New York', 'Engineer'])
dict_items([('name', 'Alice'), ('age', 26), ('city', 'New York'), ('job', 'Engineer')])
In cancer research, dictionaries can be used to store patient data, genetic mutations, and statistical results as key-value pairs. This allows for easy lookup, organization, and analysis of data.
#Let’s create a dictionary to store basic patient information, where each patient has a unique ID, and each ID maps to a dictionary containing information about the patient’s age, cancer type, and stage.
# Dictionary of patients with nested dictionaries
patient_data = {
'P001': {'age': 50, 'cancer_type': 'Lung Cancer', 'stage': 'II'},
'P002': {'age': 60, 'cancer_type': 'Breast Cancer', 'stage': 'I'},
'P003': {'age': 45, 'cancer_type': 'Melanoma', 'stage': 'III'}
}
print(patient_data)
{'P001': {'age': 50, 'cancer_type': 'Lung Cancer', 'stage': 'II'}, 'P002': {'age': 60, 'cancer_type': 'Breast Cancer', 'stage': 'I'}, 'P003': {'age': 45, 'cancer_type': 'Melanoma', 'stage': 'III'}}
You can access a patient’s information using their unique ID. To access nested data, chain the keys. For example, to retrieve the cancer type of a specific patient, you’d use the following:
Cancer type for P002: Breast Cancer
Updated stage for P003: IV
New patient data can be added using assignment, and pop() or del can remove a patient’s data.
Added new patient: {'age': 70, 'cancer_type': 'Prostate Cancer', 'stage': 'II'}
Removed patient: {'age': 50, 'cancer_type': 'Lung Cancer', 'stage': 'II'}
Dictionaries allow you to retrieve keys, values, or entire key-value pairs. Here’s how to use these methods to get an overview of the data.
keys(): Retrieves all patient IDs. values(): Retrieves all patient records. items(): Retrieves patient records as key-value pairs
Further Example
# Getting all patient IDs
print("Patient IDs:", patient_data.keys())
# Getting all patient details
print("Patient Details:", patient_data.values())
# Looping through each patient's data
for patient_id, details in patient_data.items():
print(f"Patient {patient_id} - Age: {details['age']}, Cancer Type: {details['cancer_type']}, Stage: {details['stage']}")
Patient IDs: dict_keys(['P002', 'P003', 'P004'])
Patient Details: dict_values([{'age': 60, 'cancer_type': 'Breast Cancer', 'stage': 'I'}, {'age': 45, 'cancer_type': 'Melanoma', 'stage': 'IV'}, {'age': 70, 'cancer_type': 'Prostate Cancer', 'stage': 'II'}])
Patient P002 - Age: 60, Cancer Type: Breast Cancer, Stage: I
Patient P003 - Age: 45, Cancer Type: Melanoma, Stage: IV
Patient P004 - Age: 70, Cancer Type: Prostate Cancer, Stage: II
What’s the difference?
Concept | Function | Method |
---|---|---|
Definition | A block of code that performs an action | A function that is associated with an object |
Called on | Standalone / with parameters | Called on an object (e.g., a string or list) |
Syntax | function(arg) |
object.method() |
Example | len("hello") |
"hello".upper() |
Hello, Alice!
3
PATRICK
patrick
['red', 'blue', 'green']
'PATRICK'
'PATRICK'
Both work — but “str.upper()” is a method being called directly from the class.
Navigate to the follow-along file and try the practice problems!