Records and dictionaries

A record is a data structure that groups together related items of data. These are slightly more complex than arrays as you can store more than one type of data together. For example, with a game, it could be useful to set up a data structure which collects a player's login and their score in one structure.

Creating records will vary in different languages. Python uses a data structure called 'dictionaries' that has some features of the record structure.


In this example we have set up a structure which defines a player's name and then captures their login and score as one record. We have two players, Katie and Patrick. We will use the record to capture the player names Katie and Patrick with their logins and scores. Katie's login is kat10 and her score is 124. Patrick's login is pat00 and his score is 99.

This line defines a dictionary data structure with two records of a player's login name and score. The precise use of the syntax such as ‘:’ and ‘{‘ brackets makes this possible.

>>> players = { 'Katie' : { 'login': 'kat10', 'score' : 124 }, ‘Patrick' : { 'login': 'pat00', 'score' : 99 } }

The next line adds a new record to the dictionary:

>>> players['Tom'] = { 'login': 'tom13', 'score' : 121 }

The following code should return all three records in the dictionary:

>>> players

And this line of code would remove Katie's score:

>> del players['Katie']