-
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. Data returned by CAIT's A.I. blocks are represented by either a dictionary, a list, or a combination of both. Therefore, it is essential 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. 'Imagine', and the value is the singer, 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. For example, a list called randomThings can have the following form:
[1, 2, 3, "Hello", "World", {'One': 1}]
In the randomThings list, there are 6 elements, and they are numbers, strings, and dictionary. You can retrieve or modify the elements by their index in the list.
In CAIT's Visual Programming Interface, the first element of a list starts with an index 1. The next element's index is incremented by 1, and so on. For example, for the randomThings list, if we retrieve the index 1 element, we will get the number 1, i.e.
randomThings[1] = 1
and if we retrieve the index 4 element, we will get the string "Hello", i.e.
randomThings[4] = "Hello"
However, in other programming languages such as Python, the first element of a list has an index 0. Therefore, if we retrieve the index 4 element, we will let the string "World" instead, i.e.
randomThings[4] = "World"
It is essential to understand the index rule of the programming language you use when working with the list.
In CAIT's Visual Programming Interface, you can use this data structure from the Lists category. To understand what each block in this category does, please refer to the List Block Reference
Below is an example of using these blocks with the randomThings list.
First, create a variable called randomThings, then assign it to an empty list (alternatively, you can assign it to a list with data from the beginning).
Second, start adding the data elements into the randomThings list.
We can print out the data element with index 2
We can modify the data element with index 2
We can print add a new data element to the list
Finally, we can remove an element from the list











