DoAssignment study guide

A1.6 · One-Dimensional Arrays, Indexes and Updates

Learn to create and update one-dimensional arrays in a program through clear examples and targeted practice.

Ontario Grade 11 Computer Science

Programming Concepts and Skills

ICS3U study topic A1.6

A one-dimensional array is a sequence of related values stored under one variable name. It is useful when a program needs to keep several values of the same general kind, such as scores or days of the week. You can create the sequence, refer to an item by its position, and update an item by assigning a new value at that position. This lesson uses Python 3 lists as a classroom-friendly way to represent one-dimensional arrays. Other classroom languages may use different syntax or rules.

What you will learn

  • Explain how a one-dimensional array stores related values in a sequence.
  • Create an array and identify the index used to refer to each item.
  • Update an item at a chosen index and describe how the array changes.
  • Trace short Python 3 examples to determine the array state and displayed output.

1. What a one-dimensional array represents

Imagine a row of labelled spaces, with one value in each space. The whole row has one variable name, while each space has a position called an index. A one-dimensional array is a single sequence: its items are arranged in one line, not in rows and columns.
For example, a program could keep the names of three school clubs in one array. The array lets the program refer to the collection as a whole or choose one item by its index. The values in an array should make sense as a group; in this case, each item is a club name.
In Python 3, a list is commonly used for this purpose. Python uses zero-based indexes: the first item has index 0, the next has index 1, and so on. These index numbers are positions, not counts of how many items come before the item.
  • An array stores a sequence of related values under one variable name.
  • A one-dimensional array has one sequence of positions.
  • Python 3 list indexes begin at 0.

2. Create an array and read its items

To create an array in Python 3, write the variable name, an equals sign, and the values inside square brackets, separated by commas. The values are placed into the sequence in the order written. The number of values is the array's length.
The input in the example is the three club names written in the program. The process assigns that sequence to clubs and then asks for the item at index 1. The expected output is Robotics: index 0 contains Art, and index 1 contains Robotics. This result follows from the written order and Python's zero-based indexing; the code is traced, not run.
Classroom languages may use different notation or indexing rules. When reading or writing an array, check the rules of the language being used rather than assuming every language follows Python.
  • Square brackets create a Python list, and commas separate its items.
  • An index selects one item from the array.
  • The index of the last item in a non-empty Python list is one less than its length.

3. Update an item

To update an existing item in a Python list, assign a new value to the position identified by its index. The variable name and index identify the location to change; the value on the right side becomes the new item at that location. Other positions keep their values.
For example, if temperatures stores four readings, an assignment to temperatures[2] changes the third reading. It does not change the second reading, because the second reading is at index 1. After the assignment, the array still has four items, but the value at index 2 is different.
An index must identify a position that exists in the array. For a list of length four, the valid Python indexes are 0, 1, 2, and 3. An index of 4 does not refer to a fifth existing item. When updating, match the intended position to its index before writing the assignment.
  • An indexed assignment replaces the value at that position.
  • Updating one item leaves the other positions unchanged.
  • Use an index that refers to an existing item.

4. Add an item and check the new state

A program can also update an array by adding an item to the end. In Python 3, the append operation adds one new value after the existing last item. This changes the array's length by one and gives the new item the next index.
In the worked example, the input is the initial list of three supplies and the value notebook. The process appends notebook and then displays the list. The trace shows the original items remain in order, with notebook added at the end. This is an expected result derived by following the statements, not a claim that the code was executed.
When planning an update, state what the array contains before the operation, identify exactly which position or end of the sequence changes, and then describe the resulting array. This simple tracing habit helps catch mistaken indexes and mistaken expectations about which values change.
  • Python's append operation adds one item at the end of a list.
  • Appending preserves the earlier items and increases the length by one.
  • Trace the array before and after each update.

Trace for replacing a reading

Point in the programIndex 0Index 1Index 2Index 3
Before assignment17182021
After readings[2] = 1917181921

Trace for appending a supply

Point in the programIndex 0Index 1Index 2Index 3
Before appendpencilrulererasernot present
After appendpencilrulererasernotebook

Worked example

Create and select an item

Create a list of three club names, then display the name at index 1. Determine the expected output.
Club names by Python index
Index → stored value0Art1Robotics2Music

Python indexes are zero-based; each cell shows one list item.

Python 3

clubs = ["Art", "Robotics", "Music"]
print(clubs[1])
  1. Identify the input
    The program uses the three names written in the list. No values are entered by a user.
  2. Trace the index
    Python indexes start at 0. Art is at index 0, Robotics is at index 1, and Music is at index 2.
  3. Determine the output
    The print statement selects clubs[1], so the displayed value is Robotics.
Answer: Expected output: Robotics
Check: The list has three items at indexes 0 through 2, so index 1 refers to its second item.

Worked example

Replace one existing value

A program starts with four temperature readings. Replace the value at index 2 with 19, then display the list. State the expected output.
Readings after the update
Index → stored value017118219321

The value at index 2 has been replaced; the other values are unchanged.

Python 3

readings = [17, 18, 20, 21]
readings[2] = 19
print(readings)
  1. Read the initial state
    The array begins with 17 at index 0, 18 at index 1, 20 at index 2, and 21 at index 3.
  2. Apply the update
    The assignment to readings[2] replaces 20 with 19. The values at indexes 0, 1, and 3 are not assigned new values, so they remain unchanged.
  3. Determine the output
    The print statement displays the updated list in its original order, with 19 in the third position.
Answer: Expected output: [17, 18, 19, 21]
Check: Index 2 is the third position in Python, so the third value changes while the other three values stay the same.

Worked example

Append an item

A supply list contains pencil, ruler, and eraser. Add notebook to the end, then display the list. Trace the state and give the expected output.
Supplies after appending
Index → stored value0pencil1ruler2eraser3notebook

The new item is at the end, after the three original items.

Python 3

supplies = ["pencil", "ruler", "eraser"]
supplies.append("notebook")
print(supplies)
  1. Read the starting state
    The list contains three items. Their indexes are 0 for pencil, 1 for ruler, and 2 for eraser.
  2. Add the new item
    The append operation puts notebook after eraser. The original three items stay in the same positions, and notebook receives index 3.
  3. Determine the output
    The print statement displays all four items in order, including the new final item.
Answer: Expected output: ['pencil', 'ruler', 'eraser', 'notebook']
Check: Appending one value to the original three produces a list of four items, with the appended value at index 3.

Common mistakes and how to avoid them

Using index 1 to select the first item in a Python list.
Correction: Python uses zero-based indexes, so the first item is at index 0.
Thinking that an assignment such as readings[2] = 19 changes the item at index 1.
Correction: The number inside the brackets is the index being updated. Index 2 is the third position in a Python list.
Assuming append replaces the last item.
Correction: Append adds a new item after the current last item. Use an indexed assignment to replace an existing item.
Using an index that is not present in the list.
Correction: Check the list's existing positions before selecting or updating an item.

Lesson summary

  • A one-dimensional array is a sequence of related values held under one variable name.
  • In Python 3, square brackets create a list, and indexes start at 0.
  • An indexed assignment replaces the value at that position.
  • The append operation adds a value at the end of a Python list.
  • Trace each change to check which values stay the same and which value or position changes.

Check your understanding

Question 1

In Python 3, what is the index of the first item in a list?
  1. 0
  2. 1
  3. The number of items in the list
  4. correctIndex
Show answer and explanation
0
Python list indexes begin at 0, so the first item has index 0.

Question 2

Given items = ["map", "globe", "compass"], what value is assigned by items[2] = "atlas"?
  1. The first item is replaced.
  2. The second item is replaced.
  3. The third item is replaced.
  4. correctIndex
Show answer and explanation
The third item is replaced.
Index 2 identifies the third item, so compass is replaced by atlas.

Question 3

A Python list has two items, and one value is appended. How many items does it then contain?
  1. One
  2. Two
  3. Three
  4. correctIndex
Show answer and explanation
Three
Appending one value adds it after the two existing items, giving a total of three.

Key terms

One-dimensional array
A sequence of related values arranged in one line of positions.
Index
A number used to identify an item's position in an array.
Update
A change to an array, such as replacing a value or adding an item.
Python list
A Python 3 sequence written using square brackets; it can represent a one-dimensional array in these examples.

Continue through ICS3U

View the complete ICS3U Ontario Grade 11 Computer Science curriculum and lessons

About this lesson and its review

Published by DoAssignment. This reviewed lesson follows Ontario Grade 11 Computer Science (ICS3U), expectation A1.6. It is a study resource, not an official curriculum publication.

Before publication, content is checked for structure, mathematical or chemical notation, calculations, course boundaries, and readability. Errors can still occur, so corrections are welcomed.

Official curriculum reference

Report a correction or ask a question