Programming and Computation · Module 7 of 10
Lists, Tuples, and Dictionaries
Engineering work is full of collections: a set of loads, a table of properties, a list of test results. This module covers lists and indexing, tuples, and dictionaries of key-value pairs, plus the built-in tools that summarise them.
Readiness check
This module is about collections of values. Tick only what you can do closed-notes.
- Recall an average as a sum over a count.
- Recall looking up a value in a table.
- Loop over items from Module 5.
- Recall that counting usually starts at zero in code.
- Recall a material property like Young's modulus.
The core idea
A list holds an ordered, changeable collection you access by position, starting at index 0. A tuple is like a list but fixed. A dictionary stores values under named keys, so you look them up by name. Built-ins like sum, len, and max summarise a collection in one call.
Real data comes in collections, and Python gives you three core containers. A list is an ordered, changeable sequence written in square brackets, loads = [10, 12, 14, 16, 18]; you reach an element by its index, counting from zero, so loads[0] is 10 and loads[-1] is the last item, 18. Lists can grow with append and be sliced, which makes them the default container for measurements and design cases. A tuple looks like a list but uses round brackets and cannot be changed after creation, which suits fixed groupings like an (x, y) coordinate. A dictionary stores key-value pairs in braces, props = {'steel': 200, 'aluminum': 70}, and you look a value up by its key, props['steel'], which is how you build a table of material properties or named settings. To summarise a collection you rarely write a loop by hand: the built-ins sum(loads), len(loads), and max(loads) do it in one call, so the mean is just sum(loads) / len(loads). A compact list comprehension, [x ** 2 for x in loads], builds a new list from an old one in a single readable line. Choosing the right container, and using the built-ins, is what makes data handling clean.
The skills, taught in order
Five skills handle collections cleanly.
7.1 Lists and indexing
A list in square brackets holds ordered items. Access by index from 0: loads[0] is the first, loads[-1] the last. Add with append, and take slices like loads[1:3].
7.2 Tuples
A tuple, in round brackets, is an ordered but fixed collection: point = (3, 4). Use it for groupings that should not change, and for returning several values from a function.
7.3 Dictionaries
A dictionary maps keys to values: props = {'steel': 200}, read with props['steel']. It is the natural structure for a table of named properties or a record with fields.
| Container | Access by | Good for |
|---|---|---|
List [ ] | position (index) | ordered, changeable data |
Tuple ( ) | position (fixed) | fixed groupings |
Dict { } | key (name) | named lookups |
Match the container to how you will access the data.
7.4 Built-in summaries
sum(loads), len(loads), max(loads), and min(loads) summarise a list in one call. The mean is sum(loads) / len(loads), no loop needed.
7.5 List comprehensions
A comprehension builds a new list in one line: [x ** 2 for x in loads] squares each item. It is a compact, readable alternative to a loop that fills a list.
Engineering connection: a list holds a set of measurements, a dictionary holds a material-property table, and the built-ins turn a dataset into a mean, a maximum, or a filtered subset in a line.
Worked example 1: summarising a list of loads
Given five measured loads, find the mean and the maximum.
loads = [10, 12, 14, 16, 18]
mean = sum(loads) / len(loads)
print('mean =', mean)
print('max =', max(loads))
print('first, last =', loads[0], loads[-1])
Output:
mean = 14.0
max = 18
first, last = 10 18
- ProblemFind the mean and maximum of five loads, and the first and last.
- Given / findloads = [10, 12, 14, 16, 18]. Find mean, max, first, last.
- Modelmean =
sum(loads) / len(loads); index from 0 and -1. - Solvemean = 70 / 5 = 14.0; max = 18; first = 10, last = 18.
- CheckThe values sum to 70 across 5 items, giving 14.0, and 18 is the largest.
- ConclusionBuilt-ins summarise a list without any loop, and indexing reaches any element.
Worked example 2: a material-property dictionary
Store Young's modulus for two metals and look up steel.
props = {'steel': 200, 'aluminum': 70} # GPa
print(props['steel'])
props['titanium'] = 110 # add a new entry
print(props)
Output:
200
{'steel': 200, 'aluminum': 70, 'titanium': 110}
- ProblemStore and look up material stiffness by name.
- Given / findsteel 200, aluminum 70 GPa. Find steel, then add titanium.
- ModelA dictionary keyed by material name.
- Solve
props['steel']= 200; assigning a new key adds titanium at 110. - CheckSteel's modulus is about 200 GPa, and the dictionary now holds three entries.
- ConclusionA dictionary is a named lookup table you can read and extend by key.
Misconceptions and diagnostics
| Mistake | Symptom | Diagnostic question | Correction |
|---|---|---|---|
| Index starts at 1 | Off-by-one or wrong element | "Is the first index 0?" | Indexing starts at 0; the last is -1. |
| Misspelled dict key | A key error | "Does the key exist exactly?" | Keys are exact; check spelling and case. |
| Writing a loop for a sum | Verbose code | "Is there a built-in?" | Use sum, len, max. |
| Editing a tuple | A type error | "Should this change?" | Use a list if it must change; a tuple is fixed. |
Practice ladder
Given x = [3, 5, 7, 9], print the first item, the last item, and the length.
Show answer
x[0] is 3, x[-1] is 9, len(x) is 4.
Build a dictionary of three material densities and look one up.
Show answer
For example rho = {'steel': 7850, 'aluminum': 2700, 'titanium': 4500}; rho['aluminum'] is 2700 (kg/m3).
Use a list comprehension to convert a list of stresses in pascals to megapascals.
Show answer
[s / 1e6 for s in stresses] builds the megapascal list in one line.
Store a small set of test measurements in a list and report the mean, maximum, and range in a labeled summary.
What good work looks like
A list of measurements summarised with sum/len, max, and max - min, each printed with a label and units. A good answer uses built-ins rather than hand-written loops.
Working with AI, and proving it yourself
Use AI as a tutor, not a black box
Portfolio task
Represent a small engineering dataset with the right container and summarise it with built-ins into a labeled report.
Retrieval and spaced review
Closed notes. Answer out loud, then reveal.
1. What index is the first list item?
0; the last is -1.
2. How do you access a dict value?
By its key, for example props['steel'].
3. How do you get a list's mean?
sum(loads) / len(loads).
4. List versus tuple?
A list can change; a tuple is fixed.
5. What does a comprehension do?
Builds a new list from an existing one in one line.
Textbook mapping
This module follows Hans Fangohr, Introduction to Python for Computational Science and Engineering. Use these references to read further.
| Topic in this module | Where to read more |
|---|---|
| Sequences: lists and tuples | Fangohr, Section 3.3, Sequences |
| Dictionaries | Fangohr, Chapter 3, Data Structures |
| Data types overview | Fangohr, Section 3.1, What type is it |
Chapter numbers refer to Fangohr's Introduction to Python for Computational Science and Engineering. Any recent version is equivalent for study.