6.5 - Data Structures: Lists & Arrays
What data structures are and why they are used
Data structures are ways to organise and store multiple related data values together under a single name. This approach makes it easier to manage information in a program, rather than using many separate variables for each piece of data.
Key features of data structures
- They group related values, such as a list of names or scores, into one structure.
- Different types exist, each with unique properties - for example, some have a fixed size that cannot change after creation, while others allow you to add or remove items dynamically.
By using data structures, programs can handle large amounts of data more efficiently, which is essential for tasks like storing user information or processing lists of numbers.
Arrays as a fundamental data structure
An array is a common type of data structure that holds multiple values, known as elements, in an ordered list. All elements in an array must be of the same data type, such as all strings or all integers, to keep the structure consistent.
Properties of arrays
- Elements - These are the individual data values stored in the array.
- Index - Each element has a position, starting from 0 for the first item (this is called zero-based indexing).
- Fixed size - Many arrays require you to set the size when creating them, and you cannot easily change it later.
For example, an array might store names like this: position 0 holds "Alice", position 1 holds "Bob", and so on.
Creating and assigning values to arrays
To use an array, you first create it by specifying its name and size, then assign values to its positions. This sets up the structure for storing data.
Steps to create and populate an array
- Declare the array with a name and the number of elements it can hold, e.g., an array called "users" with space for 5 elements.
- Assign values to each index using the array name and the position in square brackets.
- The first line creates the array with a fixed size of 5.
- The following lines add string values to each position, filling the array with related data like usernames.
Accessing and modifying array elements
Once an array is created, you can retrieve or change its elements by referring to their indices. This allows programs to read or update specific data without affecting the whole structure.
Retrieving elements from an array
Use the array name and the index to get a value. For example, to display the first and fifth elements:
Output:
Modifying elements in an array
Replace a value by assigning a new one to an index. For example, to change the second element:
Updated array: ["Alice", "Ben", "Charlie", "Diana", "Eve"]
This completely overwrites the old value ("Bob") with the new one ("Ben"), showing how arrays allow targeted updates.
Using loops to process arrays
Loops are useful for working with multiple elements in an array without writing separate code for each one. A FOR loop can iterate through the indices, performing the same action on each element.
How FOR loops work with arrays
- The loop runs once for each index, using a variable (like "k") to track the current position.
- This is efficient for tasks like updating every value in the array.
For example, consider an array called "scores" with these values: [10, 5, 20, 15, 30]. To add 3 to each:
- The loop starts at index 0 and ends at 4 (for 5 elements).
- In each iteration, it adds 3 to the element at position "k".
- Resulting array:
[13, 8, 23, 18, 33]
This method scales well for larger arrays, as the loop handles any size without extra code.
Other data structures similar to arrays
While arrays are fixed and require the same data type, other data structures offer more flexibility. For instance, lists in Python can grow or shrink and hold mixed data types, like numbers and strings together.
Key differences and considerations
- Lists - Similar to arrays but dynamic; you can add or remove items easily without a fixed size.
- Mixed data types - Unlike arrays, some structures (like Python lists) allow different types in one structure, e.g.,
my_list = [10, "world", 7.2] - Choosing between arrays and other structures depends on your program's needs, such as whether the data collection will change size.