Structured & Record Arrays

A structured array is a NumPy array whose elements are records built from several named fields, each with its own data type — letting you keep table-like, mixed-type data (a string name, an integer age, a float height) inside one fast ndarray. In this lesson you'll define a structured dtype, build an array from a list of tuples, read fields by name, sort by a field, and meet recarrays, which add convenient dot-access.

Learn Structured & Record Arrays in our free NumPy course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Numpy course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

A normal NumPy array holds a single type for every element. A structured array instead gives each element several named fields , each with its own type. You describe that layout with a structured dtype — a list of (name, type) tuples.

Common type codes: 'U10' is a Unicode string up to 10 characters, 'i4' is a 4-byte integer, and 'f8' is an 8-byte float.

Expected output: the field names ('name', 'age', 'height') .

Once you have a dtype, build the array from a list of tuples — one tuple per record, with the values in field order. NumPy stores all the records together in fast, contiguous memory.

Expected output: the three records and a shape of (3,) — three records, each with 3 fields.

Index a structured array with a field name to pull that whole column out as a regular array. This is where structured arrays shine: you can run normal vectorized math on a single field.

Expected output: [30 25 35] , the names array, and the mean age 30.0 .

To sort whole records by one field, pass order='fieldname' to np.sort . You can even pass a list of fields to break ties — NumPy sorts by the first, then the second, and so on.

Expected output: names in age order ['Bob' 'Alice' 'Cara'] and ages [25 30 35] .

A recarray is a structured array with a small convenience: you can read a field with attribute syntax — data.age instead of data['age'] . Create one with np.rec.array . The underlying data is identical; only the access style changes.

Expected output: the names array and the ages [30 25 35] , read with dot-access.

Replace each ___ so the program builds a structured array, reads the score field, and sorts the records by score.

Expected output: [70 95 80] then ['Mia' 'Sam' 'Leo'] . (Answers: 'i4' , 'score' , score .)

A field typed 'U5' stores only 5 characters, so 'Alexander' becomes 'Alexa' .

✅ Fix: make the width big enough, e.g. 'U20' , for your longest expected string.

❌ IndexError / wrong values when using lists instead of tuples

Each record must be a tuple like ('Bob', 25, 1.8) , not a list.

✅ Fix: wrap each record in parentheses and pass the matching dtype .

Build a structured array of people and print the name of the tallest one using np.argmax on the height field.

Lesson 19 complete — you can build table-like arrays!

You defined a structured dtype, built records from tuples, read fields by name, sorted by a field, and used a recarray for convenient dot-access.

🚀 Up next: File I/O — save and load your arrays with save, load, and savetxt.

Practice quiz

What does a structured dtype let you store in one ndarray?

  • Only floats of one size
  • Records with several named fields, each its own type
  • A single Python dictionary
  • Strings only

Answer: Records with several named fields, each its own type. A structured array gives each element named fields, each with its own data type.

How do you describe a structured dtype layout?

  • A list of (name, type) tuples
  • A single string
  • A NumPy matrix
  • A set of integers

Answer: A list of (name, type) tuples. You pass a list like [('name','U10'), ('age','i4'), ('height','f8')].

What does the type code 'U10' mean?

  • A 10-byte integer
  • 10 floats
  • A Unicode string up to 10 characters
  • 10-dimensional array

Answer: A Unicode string up to 10 characters. 'U10' is a Unicode string of up to 10 characters.

Each record in the array passed to np.array must be a what?

  • A list
  • A dictionary
  • A NumPy array
  • A tuple

Answer: A tuple. Records are tuples like ('Bob', 25, 1.8), matching the dtype field order.

How do you pull the 'age' field out as a regular array?

  • age

Answer: age. Indexing with a field name returns that whole column as a normal array.

What does np.sort(people, order='age') do?

  • Sorts only the age field in place
  • Sorts whole records by the age field
  • Deletes the age field
  • Reverses every field

Answer: Sorts whole records by the age field. order= sorts the entire records by the named field.

What is the shape of an array built from 3 records of 3 fields each?

  • (3, 3)
  • (9,)
  • (3,)
  • (1, 3)

Answer: (3,). It is (3,) — three records, each holding 3 fields, not a 2D array.

How do you create a recarray for dot-access?

  • np.array(...)
  • np.dtype(...)
  • np.records(...)
  • np.rec.array(...)

Answer: np.rec.array(...). np.rec.array builds a recarray that supports data.age attribute access.

On a recarray, how do you read the age field with dot-access?

  • age

Recarrays add attribute access, so data.age works like data['age'].

If a field is typed 'U5', what happens to 'Alexander'?

  • It raises an error
  • It stays full length
  • It is truncated to 'Alexa'
  • It becomes empty

Answer: It is truncated to 'Alexa'. 'U5' stores only 5 characters, silently truncating to 'Alexa'.