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.

01

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.
0 or 1 weak itemsContinue with this module.
2 weak itemsRevisit loops in Module 5.
3 or more weak itemsRevisit functions in Module 6.
02

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.

loads = [10, 12, 14, 16, 18]loads[0] is 10 (index from 0)props = {'steel': 200}

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 skill works when: you pick a list for ordered data and a dict for named lookups, and use the built-ins.
The skill breaks down when: indexing forgets it starts at 0, or a dict key is misspelled.
The concept. A list is accessed by position starting at 0; a dictionary is accessed by a named key. Each suits a different kind of data.
03

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.

ContainerAccess byGood 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.

04

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
  1. ProblemFind the mean and maximum of five loads, and the first and last.
  2. Given / findloads = [10, 12, 14, 16, 18]. Find mean, max, first, last.
  3. Modelmean = sum(loads) / len(loads); index from 0 and -1.
  4. Solvemean = 70 / 5 = 14.0; max = 18; first = 10, last = 18.
  5. CheckThe values sum to 70 across 5 items, giving 14.0, and 18 is the largest.
  6. ConclusionBuilt-ins summarise a list without any loop, and indexing reaches any element.
Result. mean 14.0, max 18.
05

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}
  1. ProblemStore and look up material stiffness by name.
  2. Given / findsteel 200, aluminum 70 GPa. Find steel, then add titanium.
  3. ModelA dictionary keyed by material name.
  4. Solveprops['steel'] = 200; assigning a new key adds titanium at 110.
  5. CheckSteel's modulus is about 200 GPa, and the dictionary now holds three entries.
  6. ConclusionA dictionary is a named lookup table you can read and extend by key.
Result. props['steel'] = 200 GPa.
06

Misconceptions and diagnostics

MistakeSymptomDiagnostic questionCorrection
Index starts at 1Off-by-one or wrong element"Is the first index 0?"Indexing starts at 0; the last is -1.
Misspelled dict keyA key error"Does the key exist exactly?"Keys are exact; check spelling and case.
Writing a loop for a sumVerbose code"Is there a built-in?"Use sum, len, max.
Editing a tupleA type error"Should this change?"Use a list if it must change; a tuple is fixed.
07

Practice ladder

Level 1 · Direct skill

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.

Level 2 · Mixed concept

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).

Level 3 · Independent problem

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.

Transfer task | Real engineering

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.

08

Working with AI, and proving it yourself

Use AI as a tutor, not a black box

"Explain when to use a list, a tuple, or a dictionary."
"Give me a list; I will predict sum, len, and max, then run them."
"Organise all my data." Choose the container yourself.
"Why the key error?" Check the key spelling yourself.

Portfolio task

Represent a small engineering dataset with the right container and summarise it with built-ins into a labeled report.

Must include: a list or dict chosen deliberately, at least two built-in summaries, and a labeled result.
09

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.

TodayFinish this quiz and Levels 1 and 2 of the ladder.
+1 daySummarise a dataset with built-ins from memory.
+3 daysBuild a property dictionary for your field.
+7 daysMove on to numerical arrays in Module 8.
+30 daysPick the right container without hesitation.
10

Textbook mapping

This module follows Hans Fangohr, Introduction to Python for Computational Science and Engineering. Use these references to read further.

Topic in this moduleWhere to read more
Sequences: lists and tuplesFangohr, Section 3.3, Sequences
DictionariesFangohr, Chapter 3, Data Structures
Data types overviewFangohr, 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.