# Lists, Vectors, and Maps

### Introduction

This article assumes that you have read Creating Temporary Variables. If you haven't read that yet, we highly recommend starting there.

At times, you may want a series of data that is grouped together. In fact, many of the variables provided by LoanPro are groups of data. This allows us to easily keep track of large groups without needing a separate variable for each item in the group. In Lisp, there are several types of groups, each with their own properties. This article explains the different types of groups and how to use them. Please note that if you change a group that’s assigned to a variable, it won’t change what’s stored in that variable. That means if you have a group of numbers stored in x and you remove a number from the group, x will still hold the original group.

### Lists

To create a list, use the list operator and then list all of the items you want in the list. You can have as many items in the list as you want. Here is an example of creating a list:

(list 1 2 3 4)

You can also assign a list to a temporary variable. The example below assigns the above list to the variable x.

(let [x (list 1 2 3 4)] x)

### Vectors

Vectors allow you to access elements of data by a numerical index. Vectors have a lot of built-in functions which can help you use them. To declare a vector, you just need to surround the items you want in a vector inside of square brackets like shown below:

[1 3 8 0 49 12]

You can also assign a vector to a variable like the following:

(let [x [1 2 3 4]] x)

#### Getting Items From a Vector

To get items from a vector at a specified index, you use the vector as the operator and have the index be the operand. The following example gets the second item from the vector “x”.

(let [x [1 2 3 4]] (x 2))

To get the first item from the vector, you can use the first operator with the vector as the operand. The example below evaluates to 1:

(first [1 2 3 4])

To get the last item from the vector, you can use the last operator with the vector as the operand. The example below evaluates to 4:

(last [1 2 3 4])

### Maps

Maps allow associating values with an index (sometimes called a “key”). This means that I can have a :firstname index that has the value of the first name of someone, and I can have a :lastname index that has the last name of someone. Maps are created by using curly braces and by having the format {index1 value index2 value}. Since you get to assign the indexes, it doesn’t matter what the indexes are as long as you remember them and you don’t have two of the same indexes.

Here is a sample of declaring a map:

{:a 1 :b 2}

The “:a” and the “:b” create named indexes. With the colon in front, Lisp won’t try to replace it with a variable name or with text, but instead creates a named index. This allows us to create indexes which are easier to read (such as :firstname). Additionally, maps can be assigned to variables as follows:

(let[x {:a 1 :b 2}] x)

#### Getting Items From a Map

To get a value at an index we can use the "get" operation. For the get operation, we pass in the map as the first operand and the index as the second operand like the example below displays:

(let[x {:a 1 :b 2}] (get x :b))

The above example returns 2. Additionally, we can get items at an index the same way we do it with vectors. Below returns the same result

(let[x {:a 1 :b 2}] (x :b))

To get a list of indexes we use the keys operation. It only takes one operand which is a map. This will return a list of keys used by the map. The example below will return (:a :b)

(let[x {:a 1 :b 2}] (keys x))

To get a list of all of the values in the map we use the vals operation, which takes a map as an operand. This will return a list of all values contained in the map. The below will return (1 2)

(let[x {:a 1 :b 2}] (vals x))

#### Removing Items From Maps

To remove items from a map, you use the dissoc operation. The dissoc operation takes the map to use as the first operand, and then as many indexes of items to remove as you want for the rest of the operands. Please note that this does not affect the original map and just creates a copy of the map without the specified items. The following is an example of this:

(let[x {:a 1 :b 2}] (dissoc x :a))

### Adding Items To Groups

Please note that these operations don’t affect the original group; they just create a duplicate group that has the changes applied.

(let [v (list 1 2 3 4)] (conj v 3 4 5))
(let[x {:a 1 :b 2}] (assoc x :c 3 :d 4))