### Use of Records To Store Data

In programming, the use of records for storing data allows us to use arrays more efficiently. When using an array to store data, we are restricted to a single data type, so in order to store multiple data types about the same thing we have two options: parallel arrays, or records.

### Why are records used instead of 2D Arrays?

Hover over this box to reveal the answer

### They can hold multiple data types

A 2D array can hold a table of data, but only one data type.

Parallel Arrays use multiple lists (1D arrays) where related data is stored in the same index, or position in each list.

This allows us to create a different array for each type of data.

In the example below, we are storing the names of different players in a game and their scores. TO do this, we need to store the name as a string, and the score as an integer. Using parallel arrays, in pseudocode this would look like (remember for AQA, substitute the = for ß):

`names =[“Tina”,”Gene”,”Louise”]scores=[3,8,12]OUTPUT names[0] + “scored “ + scores[0]`

This algorithm has been implemented in the python below. Try changing the code so that a loop is used to output each of the names along with the score in a concatenated string.

Parallel arrays certainly work, but can become very complex when many items of data are saved, or the data objects have many fields. In this case, it becomes more efficient (and easier for the programmer) to use a record. Using the same data, we could create the following pseudocode for a record:

`RECORD Player  name  scoreEND RECORDPlayer1 = Player("Tina",3)Player2 = Player("Gene",8)Player3 = Player("Louise",12)`

Try adjusting the Python code below so that each player and their score is output in order. You can edit and run the code directly in your browser:

In Python, the data structure that we have used to create a record is referred to as a dictionary. This makes the syntax slightly different to other programming languages.

Once a record has been created, it can then be added to a list, making an array of records. In effect, this creates a data structure that can be used like a 2D array but with multiple data types, and named columns instead of an index.

Try adjusting the Python code below to add some additional records to the array and then output each one in turn. You can edit & run the code directly in your browser:

In the same way as before, whilst other languages use similar syntax to pseudocode to create and output the elements of a record, Python syntax is a little different. Make sure that you have created revision notes for both pseudocode and your chosen programming language.