-
Notifications
You must be signed in to change notification settings - Fork 5
2.1 Tutorials on using Dictionary and List data structure
In this tutorial, you will learn how to use the Dictionary and List blocks. All data returned by CAIT's A.I. blocks are represented by either a dictionary, a list, or a combination of both. Therefore, it is important for you to fully understand how to use these blocks.
A dictionary is a data structure that acts as a collection of key-value pairs. For example, a dictionary called SongsAndNumbers can have the following form:
{
'Imagine': 'John Lennon',
'Hey Jude': 'The Beatles',
'Yesterday': 'The Beatles',
'Smells Like Teen Spirit': 'Nirvana',
'One': 1,
'Two': 2
}
In the above SongsAndNumbers dictionary, the key can be the name of a song, e.g. 'Imaging', and the value is the singer of that song, e.g. 'John Lennon'. Or the key can be the English name of a number, e.g. "One", and the value is the number itself, e.g. 1. This key and value correspondence is called key-value pair, and a dictionary can contain a multiple of these key-value pairs. In fact, the value of a key-value pair can be any data type or data structure, even being a dictionary.
In CAIT's Visual Programming Interface, you can use this data structure from the Dictionaries category. To understand what each block in this category does, please refer to the Dictionaries Block Reference
Below is an example of using these blocks with the SongsAndNumbers dictionary.
First, create a variable called SongsAndNumbers, then assign this variable to an empty dictionary.
Second, start adding the key-value pairs into the SongsAndNumbers dictionary.
We can print out the singer(value) of the song 'Yesterday'(key)
We can use the value of 'One'(key) to perform a mathematical operation
We can also change the value of a key, let's change the value of 'One' to 3, and perform addition again.
Finally, we can remove a key-value pair, note that the 'undefined' means key is not found in the dictionary.
List is a data structure that acts as a collection of objects. These objects can be primitive data types or other data structures such as dictionaries, or even lists.





