Skip to content

2.1 Tutorials on using Dictionary and List data structure

yelu728 edited this page Jan 6, 2021 · 10 revisions

In this tutorial, you will learn how to use the Dictionary and List blocks. In our design of CAIT, we only make use of these two data structures for data exchange with CAIT's A.I. blocks. Therefore, it is essential to understand how these two data structures work fully.

Dictionary

A dictionary is a data structure that acts as a collection of key-value pairs. For example, a dictionary called Musician can have the following form:

{
 'name': 'John Lennon',
 'age': 40,
 'gender': 'Male'
}

In the above Musician dictionary, the key can be any string representing this key, e.g. 'name', and the value is the musician's name, e.g. 'John Lennon'. Or you can also use a key for the musician's age, e.g. "age", and the value is a number, e.g. 40. 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, including 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 Musician dictionary.

1. Create the Musician dictionary

First, create a variable called Musician, then assign this variable to an empty dictionary.

Second, add the key-value pairs into the Musician dictionary.

2. Perform operations on the Musician dictionary

We can print out the name(value) of the Musician

We can use the value of 'age'(key) to perform a mathematical operation.

We can also change the value of a key, let's change the value of 'name' to 'Michael', and print out the name again.

Finally, we can remove a key-value pair, note that the 'undefined' means key is not found in the dictionary.

List

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.

1. Create 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.

2. Perform operations on 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