Bird
Raised Fist0
NumPydata~15 mins

Creating structured arrays in NumPy - Try It Yourself

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Creating Structured Arrays with NumPy
📖 Scenario: You work in a small company that tracks employee information. You want to store each employee's name, age, and salary in a structured way so you can analyze the data easily.
🎯 Goal: Create a NumPy structured array to hold employee data with fields for name, age, and salary. Then display the array.
📋 What You'll Learn
Use NumPy to create a structured array
Define the data type with fields: 'name' as string, 'age' as integer, and 'salary' as float
Fill the array with given employee data
Print the structured array
💡 Why This Matters
🌍 Real World
Structured arrays help store complex data like employee records, sensor data, or survey results in an organized way.
💼 Career
Data scientists and analysts often use structured arrays to handle mixed data types efficiently for analysis and reporting.
Progress0 / 4 steps
1
Create the employee data list
Create a list called employee_data with these exact tuples: ("Alice", 30, 70000.0), ("Bob", 25, 48000.0), and ("Charlie", 35, 120000.0).
NumPy
Hint

Use square brackets to create a list of tuples. Each tuple has name, age, and salary.

2
Define the structured array data type
Create a variable called dtype that defines a NumPy structured data type with fields: 'name' as 'U10' (string of max length 10), 'age' as int, and 'salary' as float.
NumPy
Hint

Use a list of tuples to define the dtype. Each tuple has the field name and the data type.

3
Create the structured array
Create a NumPy structured array called employees using np.array() with employee_data and dtype.
NumPy
Hint

Use np.array() with the data and the dtype to create the structured array.

4
Print the structured array
Print the variable employees to display the structured array.
NumPy
Hint

Use print(employees) to show the structured array.

Practice

(1/5)
1. What is the main purpose of creating a structured array in numpy?
easy
A. To create arrays with only one data type
B. To store data with different types in named fields within one array
C. To speed up numerical calculations on large arrays
D. To visualize data using plots

Solution

  1. Step 1: Understand structured arrays

    Structured arrays allow storing mixed data types in one array with named fields.
  2. Step 2: Compare options

    Only To store data with different types in named fields within one array correctly describes this purpose; others describe unrelated features.
  3. Final Answer:

    To store data with different types in named fields within one array -> Option B
  4. Quick Check:

    Structured arrays = mixed types + named fields [OK]
Hint: Structured arrays hold mixed data types with names [OK]
Common Mistakes:
  • Thinking structured arrays only hold one data type
  • Confusing structured arrays with plotting functions
  • Assuming structured arrays speed up all calculations
2. Which of the following is the correct way to define a structured array dtype with fields 'name' (string) and 'age' (integer)?
easy
A. dtype = [('name', 'U10'), ('age', 'i4')]
B. dtype = ['name': str, 'age': int]
C. dtype = {'name': 'string', 'age': 'int'}
D. dtype = [('name', str), ('age', float)]

Solution

  1. Step 1: Recall dtype syntax for structured arrays

    Structured array dtypes are defined as a list of tuples: (field_name, data_type).
  2. Step 2: Check each option

    dtype = [('name', 'U10'), ('age', 'i4')] uses correct tuple syntax with numpy string and integer types. Others use invalid syntax or wrong types.
  3. Final Answer:

    dtype = [('name', 'U10'), ('age', 'i4')] -> Option A
  4. Quick Check:

    Structured dtype = list of (name, type) tuples [OK]
Hint: Use list of (field, type) tuples for dtype [OK]
Common Mistakes:
  • Using dictionary syntax instead of list of tuples
  • Using Python types instead of numpy dtype strings
  • Mixing float type for integer fields
3. What will be the output of the following code?
import numpy as np
dtype = [('id', 'i4'), ('score', 'f4')]
data = np.array([(1, 9.5), (2, 8.0)], dtype=dtype)
print(data['score'])
medium
A. Error: KeyError
B. [1 2]
C. [(1, 9.5) (2, 8.0)]
D. [9.5 8. ]

Solution

  1. Step 1: Understand structured array creation

    The array has fields 'id' (int) and 'score' (float), with two records.
  2. Step 2: Access the 'score' field

    Accessing data['score'] returns an array of the 'score' values: [9.5, 8.0].
  3. Final Answer:

    [9.5 8. ] -> Option D
  4. Quick Check:

    Accessing field returns array of that column [OK]
Hint: Access fields by name to get column arrays [OK]
Common Mistakes:
  • Expecting full records instead of single field
  • Confusing field names causing KeyError
  • Thinking output is list of tuples
4. Identify the error in the following code that tries to create a structured array:
import numpy as np
dtype = [('name', 'U5'), ('age', 'i4')]
data = np.array([('Alice', 25), ('Bob', 30)], dtype=dtype)
print(data['age'])
medium
A. The dtype definition is missing field names
B. The tuple elements should be lists, not tuples
C. No error; code runs correctly
D. The string length 'U5' is too short for 'Alice'

Solution

  1. Step 1: Check dtype and data compatibility

    'U5' means Unicode string of length 5, 'Alice' has 5 characters, so it fits.
  2. Step 2: Verify data structure and code correctness

    Data is a list of tuples matching dtype fields; code runs without error and prints ages.
  3. Final Answer:

    No error; code runs correctly -> Option C
  4. Quick Check:

    String length matches field size; tuples allowed [OK]
Hint: Check string length matches longest string [OK]
Common Mistakes:
  • Assuming string length too short causes error
  • Thinking tuples are invalid for data input
  • Believing dtype must be dictionary
5. You have a list of employee data: [('John', 28, 50000), ('Jane', 32, 60000), ('Doe', 24, 45000)]. How do you create a structured array with fields 'name' (string, max 10 chars), 'age' (int), and 'salary' (float) to store this data?
hard
A. dtype = [('name', 'U10'), ('age', 'i4'), ('salary', 'f8')]; np.array(data, dtype=dtype)
B. dtype = [('name', str), ('age', int), ('salary', float)]; np.array(data)
C. dtype = {'name': 'U10', 'age': 'i4', 'salary': 'f8'}; np.array(data)
D. dtype = [('name', 'S10'), ('age', 'i4'), ('salary', 'f4')]; np.array(data)

Solution

  1. Step 1: Define correct dtype for fields

    Use list of tuples with field names and numpy types: 'U10' for Unicode string max 10 chars, 'i4' for int, 'f8' for float64.
  2. Step 2: Create structured array with data and dtype

    Pass data and dtype to np.array to create structured array storing all fields correctly.
  3. Final Answer:

    dtype = [('name', 'U10'), ('age', 'i4'), ('salary', 'f8')]; np.array(data, dtype=dtype) -> Option A
  4. Quick Check:

    Use list of (field, type) tuples and pass dtype [OK]
Hint: Use Unicode string 'U10' for names, correct numeric types [OK]
Common Mistakes:
  • Using Python types instead of numpy dtype strings
  • Using dictionary instead of list of tuples for dtype
  • Choosing byte string 'S10' instead of Unicode 'U10'