3. Composite Data Structures

Contents

3. Composite Data Structures#

Data Structure

Description

list

A collection of ordered, mutable items that can hold elements of different types.

tuple

An ordered, immutable sequence of items, which can also hold elements of different types.

set

An unordered collection of unique, immutable items.

dict

A collection of key-value pairs, where keys are unique and immutable, and values are mutable.

1. Lists#

A python list is a built-in container style data structure that allows you to store multiple items in a variable. Lists are *ordered, mutable (changeable) and can hold elements of different data types, including other lists. Lists use brackets [ ] to identify them

Ways of making a list#

1. Using Square Brackets#

noble_gases = [“He”, “Ne”, “Ar”, “Kr”, “Xe”, “Rn”, “Og”]

noble_gases = ["He", "Ne", "Ar", "Kr", "Xe", "Rn", "Og"]
atomic_masses = [4.0026, 20.180, 39.948, 83.798, 131.293, 222, 294]  # g/mol
atomic_numbers = [2, 10, 18, 36, 54, 86, 118]  # Atomic numbers
boiling_points = [4.22, 27.07, 87.30, 119.93, 165.03, 211.3, None]  # K (Og unknown)
print(noble_gases)
print(boiling_points)
['He', 'Ne', 'Ar', 'Kr', 'Xe', 'Rn', 'Og']
[4.22, 27.07, 87.3, 119.93, 165.03, 211.3, None]

2. list() constructor#

The list() constructor converts a single iterable item like a tuple, set, string into a list. Here we are converting a tuple into a list, which is why there are two parenthesis. The inner one is for the tuple, and the other is for the list constructor

alkanes = list((“Methane”, “Ethane”, “Propane”, “Butane”, “Pentane”, “Hexane”, “Heptane”, “Octane”, “Nonane”, “Decane”))

alkanes = list(("Methane", "Ethane", "Propane", "Butane", "Pentane",
                "Hexane", "Heptane", "Octane", "Nonane", "Decane"))

alkane_bp = list((-161.5, -88.5, -42.1, -0.5, 36.1,
                  68.7, 98.4, 125.7, 150.8, 174.1))  # Boiling points in °C

print(alkanes)
print(alkane_bp)
['Methane', 'Ethane', 'Propane', 'Butane', 'Pentane', 'Hexane', 'Heptane', 'Octane', 'Nonane', 'Decane']
[-161.5, -88.5, -42.1, -0.5, 36.1, 68.7, 98.4, 125.7, 150.8, 174.1]

Create an empty list#

Often we want to add items to a list that starts off as empty, and we need a variable defined as the list. my_sum = []

my_sum = []
print(my_sum)
[]
elements = ["hydrogen", "helium"]
print(elements)
['hydrogen', 'helium']

List Methods#

Table of List Methods#

The following table shows commonly used list methods, which are functions specific to list objects:

Method

Description

Example

append(x)

Adds an item x to the end of the list.

elements.append("Beryllium")

extend(iterable)

Extends the list by appending elements from an iterable.

elements.extend(["Boron", "Carbon"])

insert(i, x)

Inserts item x at position i.

elements.insert(1, "Neon")

remove(x)

Removes the first occurrence of x.

elements.remove("Helium")

pop([i])

Removes and returns the item at index i (last if unspecified).

elements.pop(2)

index(x)

Returns the index of x.

elements.index("Lithium")

count(x)

Returns the count of x in the list.

elements.count("Oxygen")

sort()

Sorts the list in ascending order.

elements.sort()

reverse()

Reverses the order of elements in the list.

elements.reverse()

copy()

Returns a shallow copy of the list.

new_list = elements.copy()

clear()

Removes all elements from the list.

elements.clear()


Built-in Functions That Work on Lists#

Python provides several built-in functions that work on lists:

Function

Description

Example

len(lst)

Returns the number of elements in the list.

len(elements)

min(lst)

Returns the smallest element.

min([3, 1, 4])

max(lst)

Returns the largest element.

max([3, 1, 4])

sum(lst)

Returns the sum of all elements (if numeric).

sum([3, 1, 4])

sorted(lst)

Returns a new sorted list without modifying the original.

sorted(elements)

list(iterable)

Converts an iterable (like a tuple or string) into a list.

list("Chemistry")


Examples of List Methods and Functions in Action#

# Create and print a list
molar_mass=['hydrogen',1.00784,'helium',4.002602,'lithium', 6.941, 'beryllium', 9.012182]
print(molar_mass)
['hydrogen', 1.00784, 'helium', 4.002602, 'lithium', 6.941, 'beryllium', 9.012182]
# determine the length of a list
print(len(molar_mass))
print(f"There are {len(molar_mass)} items in the list molar_Mass")
8
There are 8 items in the list molar_Mass
# use indexing to print even items (starting with zero)
print(molar_mass[0::2])
print(molar_mass[1::2])
['hydrogen', 'helium', 'lithium', 'beryllium']
[1.00784, 4.002602, 6.941, 9.012182]
# summing the masses
masses = molar_mass[1::2]
print(masses)
print(type(masses))
print(sum(masses))
[1.00784, 4.002602, 6.941, 9.012182]
<class 'list'>
20.963624
# calculate the average molar mass
ave = sum(molar_mass[1::2])/len(molar_mass[1::2])
print(ave)
5.240906
import statistics
print(statistics.mean(molar_mass[1::2]))
5.240906
noble_gases = ["He", "Ne", "Ar", "Kr", "Xe", "Rn", "Og"]
ng_atomic_masses = [4.0026, 20.180, 39.948, 83.798, 131.293, 222, 294]
ng_atomic_numbers = [2, 10, 18, 36, 54, 86, 118]  # Atomic numbers
ng_boiling_points = [4.22, 27.07, 87.30, 119.93, 165.03, 211.3, None]  # K (Og unknown)
print(f"Noble gases: \t{noble_gases} \nAtomic masses: \t{ng_atomic_masses} \
\nAtomic numbers: {ng_atomic_numbers}\nBoiling Points: {ng_boiling_points} ")
Noble gases: 	['He', 'Ne', 'Ar', 'Kr', 'Xe', 'Rn', 'Og'] 
Atomic masses: 	[4.0026, 20.18, 39.948, 83.798, 131.293, 222, 294] 
Atomic numbers: [2, 10, 18, 36, 54, 86, 118]
Boiling Points: [4.22, 27.07, 87.3, 119.93, 165.03, 211.3, None] 
# Using Indexing
print(f" The boiling point of {noble_gases[1]} is {ng_boiling_points[1]}")
 The boiling point of Ne is 27.07

For Loop and Lists#

Since a list is iterable, we can set up a for loop to go through each item and pull out a value by its index number. We will cover loops and other control structures later, but I want you to see how a we can go through each item using its index number

# Loop through each index and print the name and boiling point
noble_gases = ["He", "Ne", "Ar", "Kr", "Xe", "Rn", "Og"]
ng_boiling_points = [4.22, 27.07, 87.3, 119.93, 165.03, 211.3, None] 

for i in range(len(noble_gases)):
    if ng_boiling_points[i] is None:
        print(f"The boiling point of {noble_gases[i]} is unknown.")
    else:
        print(f"The boiling point of {noble_gases[i]} is {ng_boiling_points[i]} K.")
The boiling point of He is 4.22 K.
The boiling point of Ne is 27.07 K.
The boiling point of Ar is 87.3 K.
The boiling point of Kr is 119.93 K.
The boiling point of Xe is 165.03 K.
The boiling point of Rn is 211.3 K.
The boiling point of Og is unknown.

Combining lists and zip()#

The following code creates an empty list, then creates a list of tuples (the next topic), and then appends the tuples to the empty list

noble_gases = ["He", "Ne", "Ar", "Kr", "Xe", "Rn", "Og"]
ng_boiling_points = [4.22, 27.07, 87.3, 119.93, 165.03, 211.3, None] 

combined_list = []
for gas, bp in zip(noble_gases, ng_boiling_points):
    combined_list.append(gas)
    combined_list.append(bp)

print(combined_list)
['He', 4.22, 'Ne', 27.07, 'Ar', 87.3, 'Kr', 119.93, 'Xe', 165.03, 'Rn', 211.3, 'Og', None]

Before getting into the above code and tuples, lets try and take the average of the molar masses in the above list of repeating symbols and masses. We could try the following code, but the last value is non-numeric (None). In later modules we will see more advanced ways of handling non-numeric values

# The following gives an error due to a non-numeric value in ng_boiling_points
import statistics
print(statistics.mean(combined_list[1::2
                      <div class="alert alert-info">
cleaned_molarmass = [x for x in combined_list[1::2] if x is not None]
average = statistics.mean(cleaned_molarmass)
print(average)  
102.47500000000001

2. Tuples#

Tuples are a container data type similar to a list, but are immutable (unchangeable), that is, once created they can not be changed. Tuples are defined with parenthesis() while lists are defined with brackets[].

Key Features of Tuples#

  • Immutable – Cannot be changed after creation.

  • Ordered – Elements remain in a fixed sequence.

  • Indexed – Can access elements using indexing (like lists).

  • sliced - Can be sliced (like lists).

  • Allow duplicates – Can contain repeated values.

  • Can store multiple data types – Similar to lists.

noble_gases = ["He", "Ne", "Ar", "Kr", "Xe", "Rn", "Og"]
ng_boiling_points = [4.22, 27.07, 87.3, 119.93, 165.03, 211.3, None]

# Convert zip object to a list of tuples
zipped_list = list(zip(noble_gases, ng_boiling_points))

print("Zipped List of Tuples:")
print(zipped_list)
Zipped List of Tuples:
[('He', 4.22), ('Ne', 27.07), ('Ar', 87.3), ('Kr', 119.93), ('Xe', 165.03), ('Rn', 211.3), ('Og', None)]
print("Tuples from zip(noble_gases, ng_boiling_points):")
for pair in zip(noble_gases, ng_boiling_points):
    print(pair)
Tuples from zip(noble_gases, ng_boiling_points):
('He', 4.22)
('Ne', 27.07)
('Ar', 87.3)
('Kr', 119.93)
('Xe', 165.03)
('Rn', 211.3)
('Og', None)

Working with Tuples#

1. Accessing Tuple elements (Indexing)#

Tuples use zero based indexing

noble_gases = ("He", "Ne", "Ar", "Kr", "Xe", "Rn", "Og")

print(noble_gases[0])  # 'He'
print(noble_gases[-1]) # 'Og' (Last element)
He
Og

2. Slicing Tuples#

print(noble_gases)
print(noble_gases[1:4])  # ('Ne', 'Ar', 'Kr')  -> Extracts elements 1 to 3
print(noble_gases[:3])   # ('He', 'Ne', 'Ar')  -> First three elements
print(noble_gases[4:])   # ('Xe', 'Rn', 'Og')  -> From index 4 to end
('He', 'Ne', 'Ar', 'Kr', 'Xe', 'Rn', 'Og')
('Ne', 'Ar', 'Kr')
('He', 'Ne', 'Ar')
('Xe', 'Rn', 'Og')

3. Iterating Through a Tuple#

for gas in noble_gases:
    print(gas)
He
Ne
Ar
Kr
Xe
Rn
Og

4.Tuple Unpacking#

Python allows assigning tuples to multiple variables

element1, element2, element3 = ("H", "He", "Li")

print(element1)  # H
print(element2)  # He
print(element3)  # Li
H
He
Li

5. Using zip() to Create Tuples#

noble_gases = ("He", "Ne", "Ar", "Kr", "Xe", "Rn", "Og")
boiling_points = (4.22, 27.07, 87.3, 119.93, 165.03, 211.3, None)

zipped_data = list(zip(noble_gases, boiling_points))
print(zipped_data)
[('He', 4.22), ('Ne', 27.07), ('Ar', 87.3), ('Kr', 119.93), ('Xe', 165.03), ('Rn', 211.3), ('Og', None)]
names = ["Hydrogen", "Helium", "Lithium"]
symbols = ["H", "He", "Li"]
atomic_weights = [1.008, 4.0026, 6.94]

zipped_data = list(zip(names, symbols, atomic_weights))
print(zipped_data)
[('Hydrogen', 'H', 1.008), ('Helium', 'He', 4.0026), ('Lithium', 'Li', 6.94)]

7. Tuple unpacking and zip(*zipped_data)#

m_names, m_symbols, m_atomic_weights=zip(*zipped_data)
print(m_names)
print(m_symbols)
print(m_atomic_weights)
('Hydrogen', 'Helium', 'Lithium')
('H', 'He', 'Li')
(1.008, 4.0026, 6.94)

3. Sets#

What is a Set?#

A set is a built-in Python data type that represents an unordered collection of unique elements. Unlike lists or tuples, sets do not allow duplicate values and are defined using curly braces {} or the set() constructor.

Sets, Lists and Tuples#

  • Lists are ordered and allow duplicates.

  • Tuples are ordered and immutable.

  • Sets are unordered, do not allow duplicates, and enable fast lookups.

Introduction to Set Theory#

Set theory is a fundamental branch of mathematics that deals with collections of objects, called elements. A set is defined as a well-defined collection of distinct objects. In chemistry, we often deal with sets when categorizing elements, compounds, or reaction products.

Basic Set Notation#

  • Universal Set (U): The set containing all elements under consideration.

  • Subset (⊆): A set A is a subset of B if every element in A is also in B.

  • Proper Subset (⊂): A subset A of B where A ≠ B.

  • Union (A ∪ B): The set containing all elements that belong to either A or B or both.

  • Intersection (A ∩ B): The set containing elements common to both A and B.

  • Difference (A - B): The set of elements in A but not in B.

  • Complement (A′ or U - A): The set of elements not in A but in the universal set U.

Relationship to Boolean Algebra#

Set theory is closely related to Boolean algebra, which is used in logic and computing. The operations in Boolean algebra resemble set operations:

  • Union (A ∪ B) corresponds to logical OR (A OR B).

  • Intersection (A ∩ B) corresponds to logical AND (A AND B).

  • Complement (A′) corresponds to logical NOT (NOT A).

  • Difference (A - B) corresponds to A AND NOT B.

These relationships are particularly useful when working with Venn diagrams, logic circuits, or computational chemistry applications.

Why Use Sets in Chemistry?#

  • Uniqueness: Automatically removes duplicate values.

  • Efficient Membership Testing: Checking if an element is in a set is faster than checking in a list.

  • Set Operations: Provides powerful operations like union, intersection, and difference, useful in data analysis and scientific computing.

Creating a Set#

You can create a set using curly braces {} or the set() function:

# Creating sets
chemical_elements = {"H", "He", "Li", "Be"}
print(chemical_elements)  # Output: {'H', 'He', 'Li', 'Be'}

# Using set() constructor
unique_numbers = set([1, 2, 2, 3, 4, 4])
print(unique_numbers)  # Output: {1, 2, 3, 4}

Set Methods#

Table of Set Methods#

Here are some common methods used with sets:

Method

Description

add(x)

Adds an element x to the set.

remove(x)

Removes x from the set (raises an error if x is not present).

discard(x)

Removes x if present, but does not raise an error if absent.

pop()

Removes and returns an arbitrary element.

clear()

Removes all elements from the set.

copy()

Returns a shallow copy of the set.

union(set2)

Returns a new set containing all elements from both sets.

intersection(set2)

Returns a new set with elements common to both sets.

difference(set2)

Returns a new set with elements in the first set but not in the second.

symmetric_difference(set2)

Returns a new set with elements in either set but not both.

issubset(set2)

Checks if the set is a subset of set2.

issuperset(set2)

Checks if the set is a superset of set2.

Set Operators#

Table of Set Operators#

Operator

Symbol

Description

Example Usage

Equivalent Method

Union

`

`

Combines all unique elements from both sets.

A | B

Intersection

&

Retrieves elements common to both sets.

A & B

A.intersection(B)

Difference

-

Returns elements in the first set that are not in the second.

A - B

A.difference(B)

Symmetric Difference

^

Returns elements in either set, but not in both.

A ^ B

A.symmetric_difference(B)

Subset

<=

Checks if all elements of the first set are in the second set.

A <= B

A.issubset(B)

Proper Subset

<

Checks if the first set is a subset of the second set and not equal.

A < B

N/A

Superset

>=

Checks if all elements of the second set are in the first set.

A >= B

A.issuperset(B)

Proper Superset

>

Checks if the first set is a superset of the second set and not equal.

A > B

N/A

Functions That Work on Sets#

Table of Functions that Work with Sets#

Function

Description

len(set)

Returns the number of elements in the set.

min(set)

Returns the smallest element (for comparable elements).

max(set)

Returns the largest element (for comparable elements).

sum(set)

Returns the sum of all elements (for numeric sets).

sorted(set)

Returns a sorted list of elements in the set.

enumerate(set)

Returns an enumerate object for the set.

Activities Involving Sets#

1. Create a Set#

# Creating sets
chemical_elements = {"H", "He", "Li", "Be"}
print(chemical_elements)  # Output: {'H', 'He', 'Li', 'Be'}

# Using set() constructor
unique_numbers = set([1, 2, 2, 3, 4, 4])
print(unique_numbers)  # Output: {1, 2, 3, 4}
{'H', 'Be', 'Li', 'He'}
{1, 2, 3, 4}
for index, element in enumerate(chemical_elements):
    print(f"Index {index}: Element {element}")
Index 0: Element H
Index 1: Element Be
Index 2: Element Li
Index 3: Element He

2. Identify Unique Elements#

isotopes = ["C-12", "C-13", "C-12", "C-14", "C-13", "C-14"]
unique_isotopes = set(isotopes)
print(unique_isotopes)  # Expected Output: {'C-12', 'C-13', 'C-14'}
{'C-13', 'C-12', 'C-14'}

3. Basic Set Operations#

group1 = {"Na", "Mg", "Al", "Si"}
group2 = {"Mg", "Si", "P", "S"}

print("Union:", group1.union(group2))
print("Intersection:", group1.intersection(group2))
print("Difference (group1 - group2):", group1.difference(group2))
print("Symmetric Difference:", group1.symmetric_difference(group2))
Union: {'Mg', 'Al', 'P', 'Na', 'S', 'Si'}
Intersection: {'Si', 'Mg'}
Difference (group1 - group2): {'Na', 'Al'}
Symmetric Difference: {'Al', 'P', 'Na', 'S'}

4. Checking for Subsets and Supersets#

alkali_metals = {"Li", "Na", "K", "Rb", "Cs"}
metals = {"Li", "Na", "K", "Rb", "Cs", "Mg", "Al", "Fe"}

print("Are alkali metals a subset of metals?", alkali_metals.issubset(metals))
print("Are metals a superset of alkali metals?", metals.issuperset(alkali_metals))
Are alkali metals a subset of metals? True
Are metals a superset of alkali metals? True

Analyze Chemical Reaction#

-learn how to use union (|), intersection (&), and difference (-) with sets.

Define Reactants and Products as Sets#

We will analyze the combustion of methane (CH₄ + 2O₂ → CO₂ + 2H₂O)

# Define sets for reactants and products
reactants = {"CH4", "O2"}
products = {"CO2", "H2O"}

# Print them
print("Reactants:", reactants)
print("Products:", products)
Reactants: {'O2', 'CH4'}
Products: {'H2O', 'CO2'}
# Find common species between reactants and products
common_species = reactants & products
print("Common species in both reactants and products:", common_species)

# Find species that are in reactants but not in products
consumed_species = reactants - products
print("Species consumed in the reaction:", consumed_species)

# Find species that appear only in products
formed_species = products - reactants
print("Species formed in the reaction:", formed_species)
Common species in both reactants and products: set()
Species consumed in the reaction: {'O2', 'CH4'}
Species formed in the reaction: {'H2O', 'CO2'}

Sets and SMILES strings#

SMILES (Simplified Molecular Input Line Entry System) is a line notation (digital nomenclature) used in chemistry to represent molecular structures.

Molecule

Formula

SMILES

Flammability

Molecule

Formula

SMILES

Flammability

Methane

CH₄

C

Flammable

Ethane

C₂H₆

CC

Flammable

Water

H₂O

O

Non-flammable

Hydrogen Peroxide

H₂O₂

OO

Flammable

Carbon Dioxide

CO₂

O=C=O

Non-flammable

Carbon Monoxide

CO

[C-]#[O+]

Flammable

Ethanol

C₂H₆O

CCO

Flammable

Acetic Acid

C₂H₄O₂

CC(=O)O

Flammable

In the following cell we a create two sets, one of flammable chemicals and one of chemicals in a stockroom, with the latter having duplicates. We then use these to analyze the chemicals in our stockroom with regards to flammability.

Flammability using Set Operators#

# Set of flammable chemicals
flammable_chemicals = {"Methane", "Ethane", "Hydrogen Peroxide", "Carbon Monoxide", "Ethanol", "Acetic Acid"}

# List of stockroom chemicals (with duplicates)
stockroom_chemicals_list = ["Methane", "Water", "Ethanol", "Ethanol", "Carbon Dioxide", "Acetic Acid", "Water"]

# Convert list to set to remove duplicates
stockroom_chemicals = set(stockroom_chemicals_list)

# Identify flammable chemicals in the stockroom
flammable_in_stockroom = stockroom_chemicals & flammable_chemicals

# Identify non-flammable chemicals in the stockroom
non_flammable_in_stockroom = stockroom_chemicals - flammable_chemicals

# Display results
print("Flammable chemicals in stockroom:", flammable_in_stockroom)
print("Non-flammable chemicals in stockroom:", non_flammable_in_stockroom)
Flammable chemicals in stockroom: {'Methane', 'Acetic Acid', 'Ethanol'}
Non-flammable chemicals in stockroom: {'Water', 'Carbon Dioxide'}

Flammability using Set Methods#

# Identify flammable chemicals in the stockroom
flammable_in_stockroom = stockroom_chemicals.intersection(flammable_chemicals)

# Identify non-flammable chemicals in the stockroom
non_flammable_in_stockroom = stockroom_chemicals.difference(flammable_chemicals)

# Display results
print("Flammable chemicals in stockroom:", flammable_in_stockroom)
print("Non-flammable chemicals in stockroom:", non_flammable_in_stockroom)
Flammable chemicals in stockroom: {'Methane', 'Acetic Acid', 'Ethanol'}
Non-flammable chemicals in stockroom: {'Water', 'Carbon Dioxide'}

Compare Sets, Tuples and List#

# Define a list, tuple, and set of common chemical elements
chemical_list = ["Hydrogen", "Oxygen", "Carbon", "Nitrogen", "Oxygen"]
chemical_tuple = ("Hydrogen", "Oxygen", "Carbon", "Nitrogen", "Oxygen")
chemical_set = {"Hydrogen", "Oxygen", "Carbon", "Nitrogen", "Oxygen"}

# Print each collection
print("List:", chemical_list)
print("Tuple:", chemical_tuple)
print("Set:", chemical_set)
List: ['Hydrogen', 'Oxygen', 'Carbon', 'Nitrogen', 'Oxygen']
Tuple: ('Hydrogen', 'Oxygen', 'Carbon', 'Nitrogen', 'Oxygen')
Set: {'Hydrogen', 'Oxygen', 'Carbon', 'Nitrogen'}

Accessing Elements#

You can use indexing on Lists and Tuples but can not on a set, as a set is unordered.

# Access elements from the list and tuple
print("First element in list:", chemical_list[0])
print("First element in tuple:", chemical_tuple[0])

# Attempting to access elements in a set (this will cause an error)
try:
    print("First element in set:", chemical_set[0])
except TypeError as e:
    print("Error:", e)
First element in list: Hydrogen
First element in tuple: Hydrogen
Error: 'set' object is not subscriptable

Note: The last line Error: 'set' object is not subscriptable is a standardized error message hardcoded into CPython source code. The

except TypeError as e:
    print("Error:",e)

generated this where e is a TypeError object and python called the str() method which returned the error message.

4. Dictionaries#

In Python, a dictionary is a built-in data type that allows us to store and retrieve data using a system of key-value pairs. Unlike lists, which use numerical indices to access elements, dictionaries use unique keys to reference values. This makes them an excellent tool for organizing and retrieving data efficiently

Key Characteristics of Dictionaries#

  1. Key-Value Pairs: Each item in a dictionary consists of a key: value pair.

  2. Unordered: Dictionaries do not maintain a fixed order (before Python 3.7, but in newer versions, insertion order is preserved).

  3. Mutable: You can add, update, or remove items.

  4. Keys Must Be Unique: Duplicate keys are not allowed, but values can be duplicated.

  5. Keys Must Be Immutable: Only immutable data types (like strings, numbers, or tuples) can be used as dictionary keys.

  6. Values can be numbers, strings, lists, tuples, sets, other dictionaries, essentially any type of python object

Creating a Dictionary#

There are several ways to create a dictionary:

1. Using Curly Braces {}:#

elements = {
    "H": "Hydrogen",
    "He": "Helium",
    "O": "Oxygen"
}

2. Using dict() Constructor:#

elements = dict(H="Hydrogen", He="Helium", O="Oxygen")

3. Using zip() and dict to Combine Two Lists:#

symbols = ["H", "He", "O"]
names = ["Hydrogen", "Helium", "Oxygen"]
elements = dict(zip(symbols, names))
symbols = ["H", "He", "O"]
names = ["Hydrogen", "Helium", "Oxygen"]
elements = dict(zip(symbols, names))
print(elements)
{'H': 'Hydrogen', 'He': 'Helium', 'O': 'Oxygen'}

Table of Dictionary Methods#

Method

Description

dict.keys()

Returns a view of dictionary keys

dict.values()

Returns a view of dictionary values

dict.items()

Returns key-value pairs as tuples

dict.get(key, default)

Returns value for key or a default if key is missing

dict.update(other_dict)

Merges another dictionary into the current one

dict.pop(key)

Removes a key and returns its value

dict.clear()

Empties the dictionary


Modifying Dictionaries#

Adding or Updating Items#

elements["C"] = "Carbon"  # Adding a new key-value pair
elements["O"] = "Oxygen-16"  # Updating an existing key

Removing Items#

del elements["He"]  # Removes helium
oxygen = elements.pop("O")  # Removes and returns the value associated with "O"

In the .pop(‘key’) method we assigned that value to a new variable

elements["C"] = "Carbon"  # Adding a new key-value pair
elements["O"] = "Oxygen-16"  # Updating an existing key
del elements['He']  # Removes helium
print(elements)
{'H': 'Hydrogen', 'O': 'Oxygen-16', 'C': 'Carbon'}
oxygen = elements.pop('O')
print(elements)
print(oxygen)
{'H': 'Hydrogen', 'C': 'Carbon'}
Oxygen-16

Ontology#

In computer science and information science, an ontology is a structured framework that represents knowledge in a way that defines:

  • Entities (subjects or objects)

  • Properties (predicates that describe relationships between subjects and objects)

  • Relationships (how subjects, predicates, and objects connect in a logical framework)

A classic ontology model follows a subject-predicate-object structure, similar to:

"Hydrogen" "has neutron number" [0, 1, 2]
"Oxygen" "has atomic number" 8

This maps well to a dictionary of dictionaries in Python!

Creating a Periodic Table Ontology of Halogens#

Step 1: Define the Halogens and Their Properties#

We will structure the data using Python lists, then use zip() to transform them into a dictionary.

Here are some basic properties of the halogens:

  • Element symbols

  • Atomic numbers

  • Atomic masses (g/mol)

  • Electronegativity

  • Boiling points (Kelvin)

# List of halogen symbols
halogens = ["F", "Cl", "Br", "I", "At", "Ts"]

# Atomic numbers
atomic_numbers = [9, 17, 35, 53, 85, 117]

# Atomic masses (g/mol)
atomic_masses = [18.998, 35.45, 79.904, 126.90, 210, 294]

# Electronegativity (Pauling scale)
electronegativities = [3.98, 3.16, 2.96, 2.66, 2.2, None]  # Ts unknown

# Boiling points (K)
boiling_points = [85.03, 239.11, 332.0, 457.4, 610, None]  # Ts unknown
print(f"halogens = {halogens} \natomic number = {atomic_numbers} \natomic_masses = {atomic_masses} \
\nelectronegativities = {electronegativities}")
halogens = ['F', 'Cl', 'Br', 'I', 'At', 'Ts'] 
atomic number = [9, 17, 35, 53, 85, 117] 
atomic_masses = [18.998, 35.45, 79.904, 126.9, 210, 294] 
electronegativities = [3.98, 3.16, 2.96, 2.66, 2.2, None]

Step 2: Create a Dictionary Using zip()#

We now use zip() to create a dictionary where:

  • The keys are element symbols (e.g., "F", "Cl", etc.).

  • The values are dictionaries containing properties.

# Use zip to pair each element with its properties
halogen_properties = zip(halogens, atomic_numbers, atomic_masses, electronegativities, boiling_points)

# Create a dictionary of dictionaries
halogen_dict = {
    element: {
        "atomic_number": num,
        "atomic_mass": mass,
        "electronegativity": en,
        "boiling_point": bp
    }
    for element, num, mass, en, bp in halogen_properties
}
print(halogen_dict)
{'F': {'atomic_number': 9, 'atomic_mass': 18.998, 'electronegativity': 3.98, 'boiling_point': 85.03}, 'Cl': {'atomic_number': 17, 'atomic_mass': 35.45, 'electronegativity': 3.16, 'boiling_point': 239.11}, 'Br': {'atomic_number': 35, 'atomic_mass': 79.904, 'electronegativity': 2.96, 'boiling_point': 332.0}, 'I': {'atomic_number': 53, 'atomic_mass': 126.9, 'electronegativity': 2.66, 'boiling_point': 457.4}, 'At': {'atomic_number': 85, 'atomic_mass': 210, 'electronegativity': 2.2, 'boiling_point': 610}, 'Ts': {'atomic_number': 117, 'atomic_mass': 294, 'electronegativity': None, 'boiling_point': None}}
# Print all properties of Fluorine
print(f'halogen_dict[F] = {halogen_dict["F"]}\n')

# Get just the boiling point of Iodine
print(f"Iodine's boiling point: {halogen_dict['I']['boiling_point']} K\n")

# Check if Astatine has a known electronegativity
print(f"Astatine's electronegativity: {halogen_dict['At'].get('electronegativity', 'Unknown')}")
halogen_dict[F] = {'atomic_number': 9, 'atomic_mass': 18.998, 'electronegativity': 3.98, 'boiling_point': 85.03}

Iodine's boiling point: 457.4 K

Astatine's electronegativity: 2.2

Acknowledgements#

This content was developed with assistance from Perplexity AI and Chat GPT. Multiple queries were made during the Fall 2024 and the Spring 2025.