Module 1 · Section 6 of 10
Lesson 1.5 - Lists & Dictionaries
Target: ~9 min read - 20 min hands-on
Overview
A list is an ordered collection - a sequence of readings over time. A dictionary is key-value pairs - a lookup table, like material name mapped to its properties. List comprehensions build a new list by transforming or filtering an existing one - intimidating at first, but one of the most useful everyday patterns.
Why This Matters (PH Context)
A shared properties dictionary like the one below is the seed of a small in-house reference database - something many PH design offices keep as a shared Excel file. Storing it in Python means it can be imported into every future script instead of copy-pasted.
Code-Along
# Lesson 1.5 - Lists, dictionaries, and list comprehensions
# --- A list: an ordered sequence you can index, slice, and iterate ---
# [Electrical] one hourly demand reading (kW) per hour of a day
demand_kW = [180, 165, 150, 145, 160, 240, 410, 505, 520, 498, 470, 505,
530, 545, 560, 540, 500, 460, 430, 390, 340, 290, 240, 200]
print("Hours recorded:", len(demand_kW)) # len() = item count
# max() finds the biggest value; .index(v) finds WHERE that value first sits
print("Peak:", max(demand_kW), "kW at hour", demand_kW.index(max(demand_kW)))
print("Daily energy:", sum(demand_kW), "kWh") # sum() adds them all
# --- List comprehension: build a new list from an existing one, in one line ---
# form: [ expr for item in sequence if condition ]
# enumerate gives (index, value); keep the index where the value exceeds 500
heavy_hours = [h for h, kw in enumerate(demand_kW) if kw > 500]
print("Hours above 500 kW:", heavy_hours)
# transform version (no filter): convert every kW reading to MW
demand_MW = [round(kw / 1000, 3) for kw in demand_kW]
print("First six in MW:", demand_MW[:6]) # [:6] = first 6 items
# --- A dictionary: key -> value lookup. Here each value is itself a dict ---
materials = {
"Copper": {"density_kgm3": 8960, "resistivity_ohm_m": 1.68e-8, "k_WmK": 401, "yield_MPa": 70},
"Aluminum 6061-T6": {"density_kgm3": 2700, "resistivity_ohm_m": 3.99e-8, "k_WmK": 167, "yield_MPa": 276},
"Structural Steel A36": {"density_kgm3": 7850, "resistivity_ohm_m": 1.43e-7, "k_WmK": 50, "yield_MPa": 250},
"Concrete (f'c=28)": {"density_kgm3": 2400, "resistivity_ohm_m": None, "k_WmK": 1.7, "yield_MPa": None},
"Silicon": {"density_kgm3": 2330, "resistivity_ohm_m": 6.4e2, "k_WmK": 149, "yield_MPa": None},
}
cu = materials["Copper"] # look up one entry by its key
print(f"\nCopper: resistivity {cu['resistivity_ohm_m']} ohm-m, thermal k {cu['k_WmK']} W/m-K")
# .items() iterates key/value pairs; skip materials with no resistivity (None)
print("\nGood conductors (resistivity below 1e-6 ohm-m):")
for name, p in materials.items():
if p["resistivity_ohm_m"] is not None and p["resistivity_ohm_m"] < 1e-6:
print(f" {name}")
Expected output:
Hours recorded: 24
Peak: 560 kW at hour 14
Daily energy: 8973 kWh
Hours above 500 kW: [7, 8, 11, 12, 13, 14, 15]
First six in MW: [0.18, 0.165, 0.15, 0.145, 0.16, 0.24]
Copper: resistivity 1.68e-08 ohm-m, thermal k 401 W/m-K
Good conductors (resistivity below 1e-6 ohm-m):
Copper
Aluminum 6061-T6
Structural Steel A36
Practice Exercises
- [Mechanical] Given
rpm_list = [1200, 1800, 3600, 900, 3000], use a list comprehension to keep only shafts running at 3000 rpm or faster. - [Chemical] Add
"Titanium"(density 4506, resistivity 4.2e-7, k 22, yield 880) tomaterials, then print its density via lookup. - [Computer] Given
[("web", 240), ("db", 1100), ("cache", 90)](service, requests/s), write a list comprehension returning the names of services with load above 100.
# Try the practice exercises here
Knowledge Check
- What is the key difference between a list and a dictionary?
- What does
demand_kW.index(max(demand_kW))return? - What does
[x for x in [1, 2, 3, 4, 5] if x > 3]produce?
Answer key
- Lists are ordered sequences; dicts are key-value lookups
- The position (index) of the largest value
[4, 5]
6 / 63 sections · Course home · Join the coaching cohort