Chapter 4. Lists, Dictionaries, and Tuples
Lists and dictionaries are helpful built in objects which can aid in the organization and lookup of data. This chapter will go over the declaration, access, and use of lists and dictionaries.
4.1 - Lists
Unlike many other programming languages, Hassium has no concept of an array. Instead, Hassium has the list (type of object that can hold and index other objects). Lists are used to store and analyze data.
4.1.1 Lists - Initializing
To create a list, the [
and ]
symbols are used. Here is an example of a program that creates two lists, one holds some initial values while the other is empty.
func main () {
list list1 = [ 1, 2, "Hello, World!", 8.9 ];
list list2 = [];
println (list1);
println (list2);
}
Output:
[ 1, 2, Hello, World!, 8.9 ]
[ ]
The values inside of the []
in list1
are the initial values for the list. They can be of any type and length. When the lists are sent to println
, the values inside of each list are printed out next to each other.
4.1.2 Lists - Adding and Length
This section covers the list.add
function and the list.length
property. These are both features of Object Oriented Programming and will be covered in Chapter 7. Regardless, a small primer will be given here.
Like many programming languages, Hassium supports the .
attribute access binary operator. This operator accesses the object on the left, and gets the attribute on the right. The following program will declare two lists, then use the list.add
function on each to modify the list.
func main () {
list list1 = [ 3, 5, 6, 8 ];
list list2 = [ 2, 9, 6, 13 ];
println (list1);
println (list2);
list1.add (8);
list2.add (4);
list2.add (2);
println (list1);
println (list2);
}
Output:
[ 3, 5, 6, 8 ]
[ 2, 9, 6, 13 ]
[ 3, 5, 6, 8, 8 ]
[ 2, 9, 6, 13, 4, 2 ]
The code in the middle section of the program adds some values onto each list by accessing their add
function and calling it with the value to be added to the list. As a result, the second time the lists are printed out, they have different values.
We can also see this by using the list.length
property. list.length
returns an integer indicating the number of elements that are in that list.
func main () {
list list1 = [ 3, 5, 6, 8 ];
list list2 = [ 2, 9, 6, 13 ];
println (list1.length);
println (list2.length);
list1.add (8);
list2.add (4);
list2.add (2);
println (list1.length);
println (list2.length);
}
Output:
4
4
5
6
When the lists are first declared, they each have four elements inside. In the middle section, a single value is added onto list1
and two more values are added to list2
, increasing their values by one and two respectively.
4.1.3 Lists - Indexing
The value of a list can be accessed based on a zero-index (number system where the first number is zero) index. Say we wanted to get the first and third elements of our list1
from 4.1.2.
func main () {
list list1 = [ 3, 5, 6, 8 ];
println (list1 [0]); # prints the first element, 3
println (list1 [2]); # prints the third element, 6
}
Output:
3
6
When the []
operator is applied to the list variable, the number inside the []
is the index inside that list of the element to retrieve. Because this is a zero-index based system, the first element is at index 0
and the third element is at index 1
.
You can also modify the value inside of a list based on the index using the []
operator. Take a look at the following program.
func main () {
list list1 = [ 3, 5, 6, 8 ];
println (list1);
list1 [1] = 9;
println (list1);
}
Output:
[ 3, 5, 6, 8 ]
[ 3, 9, 6, 8 ]
The third line of main
tells list1
to mutate (or change) the second value in the list to the integer 9
, causing the list to have different values the second time it is printed out.
4.2 - Dictionaries
Dictionaries allow a programmer to store data values (the "definition") by their keys (the "definer"). They can be used to keep track of data similar to how lists operate.
4.2.1 Dictionaries - Initializing
Let's say you wanted to create a program that would store common abbreviations for countries. Since the program should associate the country name with the abbreviation, the abbreviations will be the keys, while the full names will be the values.
func main () {
dictionary countries = { "US" : "United States", "DE" : "Germany", "RO" : "Romania" }
}
All this program does is declare a new dictionary using the {}
dictionary initialization operator. The initial values for the dictionary are separated by a ,
comma. The key is on the left hand side of the :
and the value for that key is on the right hand side.
4.2.2 Dictionaries - Indexing
To access the value of a key in a dictionary, you can use the []
dictionary access operator. The data that goes inside of the []
is the key, causing the operator to return the value for that key.
func main () {
dictionary countries = { "US" : "United States", "DE" : "Germany", "RO" : "Romania" }
println (countries ["DE"]);
println (countries ["US"]);
}
Output:
Germany
United States
4.2.3 Dictionaries - Adding
To add a value to a dictionary, you can assign to the dictionary using the []
dictionary assignment operator. The data in the []
will be the key, and the data on the right hand side of the =
will be the value.
func main () {
dictionary countries = { "US" : "United States", "DE" : "Germany", "RO" : "Romania" }
println (countries ["DE"]);
println (countries ["US"]);
countries ["AU"] = "Australia";
println (countries ["AU"]);
}
Output:
Germany
United states
Australia
4.3 - Tuples
Tuples are similar to lists in many ways, the main difference being that they are non-mutable. This means that when a tuple has been created, it will hold that value forever. This makes tuples useful for being passed to and from functions as it prevents bad coding practices.
4.3.1 Tuples - Initializing
Tuples are denoted using the open and close parentheses ()
and hold a comma separated list of values. Here is a
sample program that creates and prints a tuple to the screen:
func main () {
tup = ( 4, 8, 15, "16", "twenty three", 4.2);
println (tup);
}
Output:
( 4, 8, 15, 16, twenty three, 4.2 )
4.3.2 Tuples - Indexing
Tuples are indexed the same way as a list, using the unary []
operator. Here is an example:
func main () {
tup = ( 4, 8, 15, "16", "twenty three", 4.2);
println (tup [1]);
println (tup [3]);
}
Output:
8
16
Chapter 4 - Exercises
Exercise 4.1
Create a program that declares a dictionary that keeps track of names and birth dates.
Exercise 4.2
Create a program that declares a list holding all the odd numbers from 0 to 20.