# Working with arrays and dictionaries

**Array** —  is a data structure that stores a collection of values (called array elements), each identified by an index. It is defined using square brackets \[]. For example, the statement q = \[] means that the variable q holds an empty array. If q = \[2, 5, 7, 4, 9], then the array contains 5 elements — this is its length. The indices of the elements, which represent their positions in the array, start from 0. So, the index of the element "2" is 0, and the index of the element "4" in our array is 3.

<figure><img src="/files/LCZhmcJU8kO0YNTbQPs5" alt=""><figcaption></figcaption></figure>

**Example:** array=\[1,2,3,4] or array1=\["mother","father","son","daughter"]

**An example of accessing an array element:** array\[1] (returns the value 2)

**Dictionary** — is a data structure that represents a specially organized set of elements which store data. All data is stored in the form of key-value pairs. Access to the data elements is performed using the key. A key always need to be unique within a single dictionary, while the data (values) can be duplicated if necessary.

**Example:** {"SLU":12345,"Name":"Men's t-shirt, white","Size":50,"Price":1000}

**An example of accessing an array element:** dictionary\["Array"] (returns the value 12345)

Both arrays and dictionaries can have complex or nested structures — that is, they can contain one another. For example:

an array of dictionaries: products=\[{"SKU":12345,"Name":"Men's t-shirt, white","Size":50,"Price":1000},{"SKU":12346,"Name":"Men's t-shirt, white","Size":52,"Price":1000}]

&#x20;The expression `s = {}` means, that the variable s holds a dictionary that contains no elements.We can put a key-value pair in it, or even several: `s = {"key1":"value1", "key2":"value2","key3":"value3"}` Each key and value is enclosed in quotation marks, a colon is placed between the key and the value, and key-value pairs are separated by commas.

Arrays and dictionaries can be nested. Let’s look at an example:

`rainbow = [red, orange, yellow, green, light blue, blue, purple]` - this is an array.

If we write&#x20;

rainbow = {"every":"red","hunter":"orange","wants":" yellow","know":"green","where":"light blue","sits":"blue","pheasant":"purple"}

\- we get a dictionary

If we look closely at the rainbow, we can see, for example, that the light blue color is more complex and consists of turquoise, light blue, and dark blue. Let’s write: `light blue = [turquoise,light blue, dark blue]`. Now let’s embed our array “blue” into the dictionary “rainbow”:

rainbow = {"first\_color":"<mark style="background-color:yellow;">red</mark>","<mark style="background-color:blue;">second\_color</mark>":"<mark style="background-color:yellow;">orange</mark>","<mark style="background-color:blue;">third\_color</mark>":"<mark style="background-color:yellow;">yellow</mark>","<mark style="background-color:blue;">forth\_color</mark>":"<mark style="background-color:yellow;">green</mark>","<mark style="background-color:blue;">sixth\_color</mark>":"<mark style="background-color:yellow;">blue</mark>","<mark style="background-color:blue;">last\_color</mark>":"<mark style="background-color:yellow;">purple</mark>"}

So how do we specify the address of an element, for example “turquoise”?&#x20;

And in arrays, addressing is done by <mark style="background-color:yellow;">**index**</mark>, so the address of the element "turquoise" is 0.

&#x20;Sum up this: **\["where"]\[0]** - is the address of the element "turquoise".

<div data-with-frame="true"><figure><img src="/files/zWJvEkCqWv0RS05S9tBw" alt=""><figcaption></figcaption></figure></div>

<div data-with-frame="true"><figure><img src="/files/JxZxqNHdX01UrILNw4SW" alt="" width="375"><figcaption></figcaption></figure></div>

## How to work with addressing (JSON)

When receiving responses from third-party services via API, we most often get JSON, which represents a dictionary. Quite often, we need to store a specific value from this dictionary into a variable.

The best way to understand the principle of addressing in JSON is through an example:<mark style="background-color:green;">{</mark><mark style="background-color:orange;">"suggestions"</mark>: <mark style="background-color:red;">\[{"value": "Dirham", "unrestricted\_value": "Dirham", "data": {"code": "784", "strcode": "AED", "name": "Dirham", "country": "UAE"}}]</mark><mark style="background-color:green;">}</mark>&#x20;

Here we have a dictionary containing a single key-value pair **suggestions** - the key for the array: `[{"value": "Dirham", "unrestricted_value": "Dirham", "data": {"code": "784", "strcode": "AED", "name": "Dirham", "country": "UAE"}}]`

The value of the **suggestions** key is an array with only one element — a dictionary:  `{"value": "Dirham", "unrestricted_value": "Dirham", "data": {"code": "784", "strcode": "AED", "name": "Dirham", "country": "UAE"}}` **suggestions|0** - is the key to the first (and only) element of the array.`{"value": "Dirham", "unrestricted_value": "Dirham", "data": {"code": "784", "strcode": "AED", "name": "Dirham", "country": "UAE"}}`

**suggestions|0|value** - is the key to the value "Belarusian ruble"

{"suggestions": \[{"<mark style="background-color:orange;">value</mark>": "Dirham", "unrestricted\_value": "Dirham", "data": {"code": "784", "strcode": "AED", "name": "Dirham", "country": "UAE"}}]}

The longest key path in this JSON is: **suggestions|0|data|strcode** - is the key for AED

<div data-with-frame="true"><figure><img src="/files/t6YouWb9Ftw66lkOaxqJ" alt="" width="563"><figcaption></figcaption></figure></div>

{"suggestions": \[{"value": "Dirham", "unrestricted\_value": "Dirham", "data": {"code": "784", "<mark style="background-color:orange;">strcode</mark>": "<mark style="background-color:red;">AED</mark>", "name": "Dirham", "country": "UAE"}}]}

Keys are separated by a vertical bar. If the JSON contains an array, access to its elements is done by index, starting from 0, and is also written using a vertical bar. Array indexing starts at 0.

In addressing, numbers are treated the same as strings and vice versa. For example, '6' and 6 are considered equal.

In addition to API responses, arrays and dictionaries can also be used to conveniently store data.

For example, if you need to restrict access to a specific section of a bot to certain users, you can store their IDs in an array, place it in a global project variable, and use it in block conditions or arrow logic to check whether the user’s ID is included in the array.

Another example is when you need to store, the number of points for each player in a team game. You can use a dictionary for this, where the keys are user IDs and the values are their points.

Sometimes, it is necessary to modify an array or a dictionary. The functions described in this article are designed for exactly that purpose. Most of the methods described below work with both arrays and dictionaries.

## Working with arrays

### How to create an array

Creating array - declaring an array

**array\_name = \[]**

### How to clear an array

Clearing an array is essentially the same as declaring an empty array:

**array\_name = \[]**

### How to access an array element

<details>

<summary>Description</summary>

**name\[index] -** accessing an array element by index or by value

</details>

<details>

<summary>Example</summary>

Let's look at some examples of working with arrays:

**Example of accessing an array element by its index:**

<div data-with-frame="true"><figure><img src="/files/jaJjgFxFIeDEsaUZuMlw" alt="" width="563"><figcaption></figcaption></figure></div>

#### Example of getting the last array element:

<div data-with-frame="true"><figure><img src="/files/3sGnuv2LYmGNlI6QmkHZ" alt="" width="563"><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example to copy</summary>

```
/*Accessing by index*/
arrayI = [1, "2", 3, 4, 5]
k=arrayI[3]
/*Accessing by index from a nested array*/
arrayV = [[1, "2", 4, 5], "2", 3, 4, 5]
v=arrayV[0][1]

/*Example of getting the last element*/
array = [1, "2", 3, 4, 5]
/*Getting the last element of the array*/
last = array[-1]
```

</details>

### How to replace a value in an array

<details>

<summary>Description</summary>

**name\[index] =** **value** - replacing an array element by a given index

</details>

<details>

<summary>Example</summary>

**Example:**

To replace the value of a specific array element, use the following syntax: *array\_name\[index] = value*

<div data-with-frame="true"><figure><img src="/files/PEhB8iCBG359Cl12qEZS" alt="" width="563"><figcaption></figcaption></figure></div>

Answer:

<div data-with-frame="true"><figure><img src="/files/tAF3U9RFHltMMHXp3iQE" alt="" width="375"><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example to copy</summary>

```
array = [1, "2", 3, 4, 5]

/*Replace in array\dictionary*/
array[2] = 888

/*Add in dictionary   dicts['key'] = 'VVVVVVVVVVV'*/
dicts['m']=new

/*Replace in array\dictionary to the number*/
array[3] = int('888')
dicts['a'] = int('555')
```

</details>

### How to check whether an element is in an array

<details>

<summary>Description</summary>

**in\_array(mass, value)** - to check if an element exists in an array.

**Parameters:** \ <mark style="color:red;">!</mark> **mass -** array \ <mark style="color:red;">!</mark> **value -** value for searching

Returns True or False depending on whether the value is found or not.

</details>

<details>

<summary>Example</summary>

Example:

<div data-with-frame="true"><figure><img src="/files/rXVLFMpznvSYOoZdnxUE" alt="" width="563"><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example to copy</summary>

```
s = ["Mike", "Peter", "Helen"] 
q = if(in_array(s, 'Ann'), 'Found', 'Another string')
```

</details>

### How to find out array length

<details>

<summary>Description</summary>

**arr\_len(mass)** - to find out array length

Parameter:\ <mark style="color:red;">!</mark> **mass -**  *array*&#x20;

Result: Returns number - array length.

{% hint style="warning" %}
Be careful when passing parameters to the function! If called without parameters, it returns 0; if the parameter is neither an array nor a dictionary, it returns -1.
{% endhint %}

</details>

<details>

<summary>Example</summary>

Example of usage:

<div data-with-frame="true"><figure><img src="/files/K560tyXbZKGVd4UIrUor" alt=""><figcaption></figcaption></figure></div>

Result:

<div data-with-frame="true"><figure><img src="/files/optb10dtmWOC0iwFMaU5" alt="" width="375"><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example to copy</summary>

```
s = ["Mike", "Peter", "Helen"] 
q = if(arr_len(s) > 5, 'Group is full', 'Join our team!')
```

</details>

### How to insert an element at the end of an array?

<details>

<summary>Description</summary>

**append(mass, element, priznak)** - to insert an element at the end of an array.&#x20;

Parameters:

<mark style="color:red;">!</mark> **mass -** array\ <mark style="color:red;">!</mark> **element -** element to insert\
**priznak** - flag indicating whether it’s an array or a dictionary

Returns the array with the value added at the end. To update the original array, use the assignment: **mass = append(mass, element, priznak)**&#x20;

{% hint style="warning" %}
&#x20;By default, data is inserted as strings. **If you need to insert an array or dictionary, pass an additional parameter True**. This indicates that you are inserting JSON.
{% endhint %}

</details>

<details>

<summary>Example</summary>

Example of usage:

<div data-with-frame="true"><figure><img src="/files/0OIMXUQBi4nFegzmAh7Z" alt="" width="563"><figcaption></figcaption></figure></div>

<div data-with-frame="true"><figure><img src="/files/SLt3aktnkauDTIh0suLi" alt="" width="375"><figcaption></figcaption></figure></div>

Example of adding to and removing from an array:

<div data-with-frame="true"><figure><img src="/files/8CORrFKn8ipj7BbnY9OA" alt="" width="563"><figcaption><p>In this example, an element is added to the project.vibpzdr array, and the value <code>p</code> is removed from the project.pzdr array</p></figcaption></figure></div>

Example of creating an array with arrays inside:

<div data-with-frame="true"><figure><img src="/files/AknXnOVmZPmlvIh3Az0H" alt="" width="563"><figcaption></figcaption></figure></div>

The result of the function execution:

<div data-with-frame="true"><figure><img src="/files/hv3odpUEbo0JAAXF0kYq" alt="" width="375"><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example to copy</summary>

```
s = ["Mike", "Peter", "Helen"]
q = append(s, 'Max')
```

</details>

### How to insert a value at a specific position in an array

<details>

<summary>Description</summary>

**insert(mass, index, value, priznak)** - to insert an element into a specific array position.

Parameters: \ <mark style="color:red;">!</mark> **mass -** array\ <mark style="color:red;">!</mark> **index -** insert position\ <mark style="color:red;">!</mark> **value -** value\
**indicator** - Indicator of adding to an array or a dictionary.

Result:\
Returns an array with the value added at the specified position. In other words, to add the value to the same array, use the command in the following form:  **mass = insert(mass, index, value, indicator)**&#x20;

{% hint style="warning" %}
By default, data is inserted as strings. **If you need to insert an array or dictionary, pass an additional parameter True**. This indicates that you are inserting JSON..
{% endhint %}

</details>

<details>

<summary>Example</summary>

Example:

<div data-with-frame="true"><figure><img src="/files/2VVNJyzSFwX0if3bBDzC" alt="" width="563"><figcaption></figcaption></figure></div>

Result:

Let's look at a more complex example - adding a dictionary t to an array s:

<figure><img src="/files/tgxunOn5UgcfkSNzKsf6" alt=""><figcaption></figcaption></figure>

In the function, we indicated that we want to add a dictionary to position 1. Let's look at the result:

<figure><img src="/files/HVH4lfS2RvMoh65Cl6VW" alt=""><figcaption></figcaption></figure>

</details>

<details>

<summary>Code example to copy</summary>

```
s = ["Mike", "Peter", "Helen"]
q = insert(s, 1, 'Max')
```

</details>

### How to delete element from array

del() | del | remove()

<details>

<summary>Description</summary>

<mark style="background-color:blue;">**By index**</mark>

**del(mass, key)** - removes an element from the array by its index.&#x20;

Parameters: \ <mark style="color:red;">!</mark> **mass -** array name;\ <mark style="color:red;">!</mark> **key -** the index of the value to be removed

Returns a new array with the element removed; the original array remains unchanged. To delete and update the same array, use the command like: \
**mass = del(mass, key)**

{% hint style="warning" %}
If your array contains numbers and you want to remove an element by value, use the remove() function.&#x20;
{% endhint %}

**del name\['index']** - removing a value from an array by index

Parameters: \ <mark style="color:red;">!</mark> **name-** array name;\ <mark style="color:red;">!</mark> **index-** the index of the value to be removed

<mark style="background-color:blue;">By value</mark>

**remove(mass, value)** - to delete a value from an *array*.&#x20;

Parameters: \ <mark style="color:red;">!</mark> **mass -** array name; \ <mark style="color:red;">!</mark> **value -** the value to remove from the array

Result:\
Returns a modified array, leaving the original array unchanged. To remove an element and update the same array, use the command in the following form:\
**mass = remove(mass, key)**

</details>

<details>

<summary>Examples</summary>

Example of removing an element by index:

<div data-with-frame="true"><figure><img src="/files/5EEHaLyVW4wkK5lnEhov" alt="" width="563"><figcaption></figcaption></figure></div>

<div data-with-frame="true"><figure><img src="/files/AkzFnBaPkbJYjJ61H0rt" alt="" width="375"><figcaption></figcaption></figure></div>

Example of removing an array element by its value:

<div data-with-frame="true"><figure><img src="/files/fFxrcMW0VTd4phpD4d5L" alt="" width="563"><figcaption></figcaption></figure></div>

<div data-with-frame="true"><figure><img src="/files/Y6SJXPnTggJBLPmc2yn9" alt="" width="375"><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example to copy</summary>

```
/*Removing an array element by its index:*/
array = ["1",2, 3, "4"]
del array['1']

array1 = ["1",2, 3, "4"]
array1=del(array1,0)


/*Removing an array element by its value:*/
array = ["1",2, 3, "4"]
array=remove(array, '4') 

array1 = ["1",2, 3, "4"]
array1=remove(array1, 2) 
```

</details>

### How to get the position of an element in an array

<details>

<summary>Description</summary>

**index(mass, value)**

Parameters: \ <mark style="color:red;">**!**</mark> **mass -** array name \ <mark style="color:red;">**!**</mark>**&#x20;value -** the value whose position needs to be determined.

If the element is not in the array, the function will return -1.

</details>

<details>

<summary>Examples</summary>

Example of determining the position of an element in an array:

<div data-with-frame="true"><figure><img src="/files/bXLl9ZulcTFl57rWpRJT" alt="" width="563"><figcaption></figcaption></figure></div>

Let's take a closer look to the result in detail:

<div data-with-frame="true"><figure><img src="/files/v4Ev1Q9GYMDU2UBSjsz0" alt="" width="375"><figcaption></figcaption></figure></div>

As we can see, since the number 5 is not in the array, the function returned -1.

</details>

<details>

<summary>Code example to copy</summary>

```
array = ["1",2, 3, "4"]
search1=index(array,"1")
search2=index(array,5)
```

</details>

### How to convert an array to human-readable text

<details>

<summary>Description</summary>

**massive\_to\_text(massive, header, numbered,delimiter1,delimiter2)**

Parameters:

<mark style="color:red;">**!**</mark>**&#x20;massive** – the array to be displayed,\
**header** – a title that will appear at the beginning of the output\
**numbered** – if any value is passed, the array elements will be numbered\
**delimiter1** – the character placed at the end of each element line (default is ‘;’),\
**delimiter2** – сthe character used after the item number when numbering is enabled (default is ‘)’)

</details>

<details>

<summary>Examples</summary>

Simple example:

As a result, the array will be displayed as a numbered list:

<div data-with-frame="true"><figure><img src="/files/WvbDXkLwvAnE58xeinGm" alt="" width="375"><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example to copy</summary>

```
Array1 = [1, 2, 3, "a", "b", "c"] 
text = array_to_text(Array1, 'title', 1) 
```

</details>

### How to exclude one array from another

<details>

<summary>Description</summary>

**except\_arr(mas1, mas2)**&#x20;

Parameters:

<mark style="color:red;">**!**</mark>**&#x20;mas1** - the array from which elements will be excluded,\ <mark style="color:red;">**!**</mark>**&#x20;mas2** - the array containing elements to be excluded

</details>

<details>

<summary>Examples</summary>

Let's look at the example:

<div data-with-frame="true"><figure><img src="/files/PcgzH9iiQwT3Roi5EPJA" alt="" width="563"><figcaption></figcaption></figure></div>

<div data-with-frame="true"><figure><img src="/files/X0bN0UpsitBnMjBqTJYg" alt="" width="375"><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example to copy</summary>

```
s1 = [1, 2, 3]
s2 = [2, 3]
s3 = except_arr(s1, s2)
```

</details>

### How to find the intersection of arrays

<details>

<summary>Description</summary>

**cross\_arr(mas1, mas2)**

Parameters:

<mark style="color:red;">**!**</mark>**&#x20;mas1** - the array to search in\ <mark style="color:red;">**!**</mark>**&#x20;mas2** - the array containing elements to look for

</details>

<details>

<summary>Examples</summary>

<div data-with-frame="true"><figure><img src="/files/cUw4SmbG0vszH8qRZRZh" alt="" width="563"><figcaption></figcaption></figure></div>

<div data-with-frame="true"><figure><img src="/files/CB8hIOp2ts3cL9H1iJgi" alt="" width="375"><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example to copy</summary>

```
s1 = [1, 2, 3]
s2 = [2, 3]
s3 = cross_arr(s1, s2)

mas1 = ["didn't", "except,", "that", "everything", "is", "so", "simple.", "thanks"]
mas2 = ["everything", "thanks"]
mas3 = cross_arr(mas1, mas2)
```

</details>

### **How to merge arrays**

<details>

<summary>Description</summary>

There is no built-in function specifically for merging arrays, but it can be easily done:

To combine arrays, perform string concatenation, then replace ']\[' with a comma  ','

</details>

<details>

<summary>Examples</summary>

This is example in the Calculator field

<div data-with-frame="true"><figure><img src="/files/CnJCNO9FrblofZ1pUrlB" alt="" width="563"><figcaption></figcaption></figure></div>

This is example a bot working

<div data-with-frame="true"><figure><img src="/files/lMvCkLMyjYpjTm1WwphM" alt="" width="375"><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example to copy</summary>

```
a = [1,2,3,4] 
b = [5,6,7] 
sum = a + b 
answer = replace(sum, '][', ',') 
```

</details>

### How to sum up the elements of an array

<details>

<summary>Description</summary>

**sum\_array(array)**

**Parameters:**

**array** - the array whose elements need to be summed

{% hint style="warning" %} <mark style="color:red;">**Attention!**</mark>**&#x20;The function works with arrays of a specific format.**

Accepted formats: - \[1,2,3,4] or ‘\[1,2,3,4]’. \
If the array contains numbers represented as strings, they need to be enclosed in double quotes—for example, \[1,2,3,”-4”].  \
If the array contains alphabetic strings, the calculation will fail.

Example of incorrect usage:\
mas = \[1,2,3,"a"] \
result = sum\_array(mas)

This will result in an error: **array has unsupported elements**
{% endhint %}

</details>

<details>

<summary>Examples</summary>

<div data-with-frame="true"><figure><img src="/files/BDgCkXFW7ZzzbZ11tV8W" alt="" width="563"><figcaption></figcaption></figure></div>

Result:

<div data-with-frame="true"><figure><img src="/files/ekv8HD8exnJVvOVXqzFG" alt="" width="563"><figcaption></figcaption></figure></div>

<div data-with-frame="true"><figure><img src="/files/uayalil5aonhEEVlmxy5" alt="" width="563"><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example to copy</summary>

```
mas = [1,2,3,4] 
result = sum_array(mas) 
```

</details>

### How to shuffle the elements of an array

<details>

<summary>Description</summary>

**shuffle\_massive(massive**

Parameters:

**massive** - the array whose elements need to be shuffled.

</details>

<details>

<summary>Examples</summary>

<div data-with-frame="true"><figure><img src="/files/YDTfaCpbQZhdS6Nn9Wvr" alt="" width="563"><figcaption></figcaption></figure></div>

Function execution results:

<div data-with-frame="true"><figure><img src="/files/8cQ4dhWfjbvFM6bGP36C" alt="" width="375"><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example to copy</summary>

```
Massive1 = [1, 2, 3, "a", "b", "c"]
Massive2 = shuffle_massive(Massive1) 
```

</details>

### For sorting arrays and dictionaries&#x20;

sort() | sort\_by\_value()

<details>

<summary>Description</summary>

<mark style="background-color:blue;">**Sorting in an array or  a dictionary**</mark>&#x20;

**sort(mass, b)** - sorts an array by value, and a dictionary by key

Parameters:

<mark style="color:red;">**!**</mark>**&#x20;mass** - array/dictionary\
**b** - sorting order (False – ascending (default), True – descending)

<mark style="background-color:blue;">**Sorting dictionary by its value**</mark>

**sort\_by\_value(dict, b) -** sorts a dictionary by values.

Parameters:\ <mark style="color:red;">**!**</mark>**&#x20;dict**- dictionary\
**b** - sorting order (False – ascending (default), True – descending)

</details>

<details>

<summary>Example</summary>

Example of sorting an array in descending order and a dictionary in ascending order:

<div data-with-frame="true"><figure><img src="/files/pUZdDZoIAFTRSFQIqE0P" alt="" width="563"><figcaption></figcaption></figure></div>

<div data-with-frame="true"><figure><img src="/files/Cvj1IXMECYzb2YYLL413" alt="" width="375"><figcaption><p>sorting result</p></figcaption></figure></div>

Sorting a dictionary by values:

<figure><img src="/files/OlU8Zf3jWEzMBQ73GTEU" alt="" width="563"><figcaption></figcaption></figure>

<figure><img src="/files/EPvA50yLXFCoIoZaIp6D" alt="" width="375"><figcaption><p>sorting result</p></figcaption></figure>

</details>

### Converting an array/dictionary to buttons

**tools\_make\_button\_str\_checker() | tools\_check\_user\_input()**

<details>

<summary>Description</summary>

<mark style="background-color:blue;">**Converting an array/dictionary to buttons**</mark>

tools\_make\_button\_str\_checker(values\_list, key, in\_line, button\_type, checker\_with\_numbers)

Parameters:

<mark style="color:red;">**!**</mark> values\_list - an array of strings or dictionaries whose data will be used to build a keyboard or a numbered list

key - the key by which selection will be made from an array of dictionaries

in\_line - number of buttons per row (default: 1)

button\_type - type of buttons (default: reply keyboard). \
Possible values:\
0 - reply keyboard,\
1 - iinline keyboard (buttons in text)

checker\_with\_numbers - whether to add button numbers to the "checker" array. \
Possible values:\
0 - do not add numbers, \
1 - add numbers (default: 1 – add numbers)

Function result — a dictionary of the form:

{"**numbered\_list**":"1. T-shirts\n2. Shorts\n3. Socks\n4. Caps\n","**buttons**":\[{"type":"inline","text":"T-shirts","line":0,"index\_in\_line":0},{"type":"inline","text":"Shorts","line":0,"index\_in\_line":1},{"type":"inline","text":"Socks","line":1,"index\_in\_line":0},{"type":"inline","text":"Caps","line":1,"index\_in\_line":1}],"**checker**":"T-shirts;1;Shorts;2;Socks;3;Caps;4;"}

The dictionary values can then be inserted into fields in the constructor:

<div data-with-frame="true"><figure><img src="/files/mFmtR2J4ek755QjJryUq" alt="" width="563"><figcaption></figcaption></figure></div>

<div data-with-frame="true"><figure><img src="/files/jXvf56JYX4j5vyJWBctA" alt="" width="375"><figcaption></figcaption></figure></div>

<mark style="background-color:blue;">**Getting a dictionary value based on client selection**</mark>&#x20;

**tools\_check\_user\_input(values\_list, user\_input, key, return\_key)**&#x20;

Parameters:

<mark style="color:red;">**!**</mark>**&#x20;values\_list** - an array of strings or dictionaries whose data will be used to build a keyboard or a numbered list\ <mark style="color:green;">Dictionary example:</mark> \[{"text":"T-shirts","price":100},{"text":"Shorts","price":150},{"text":"Socks","price":20},{"text":"Caps","price":50}]\ <mark style="color:red;">**!**</mark>**&#x20;user\_input** - the value entered by the user from one of the values in values\_list\ <mark style="color:green;">Value example:</mark> Caps\
**key** - кthe key used for selection from the values\_list array of dictionaries\ <mark style="color:green;">Key example:</mark> text\
**return\_key** -  the value returned for the specified key from values\_list\ <mark style="color:green;">Return value example:</mark> price

</details>

<details>

<summary>Example</summary>

Let’s break down the function usage with a shopping cart example:

1.Define an array and convert it into a numbered list, buttons, and a list of possible values (for messengers without buttons) using tools\_make\_button\_str\_checker() function.

<div data-with-frame="true"><figure><img src="/files/aB32jpHTC6j3JXV8MqD0" alt=""><figcaption></figcaption></figure></div>

<div data-with-frame="true"><figure><img src="/files/ZQs05IhA5JfDfReeroa0" alt="" width="375"><figcaption><p>Result of using <strong>tools_make_button_str_checker()</strong> function</p></figcaption></figure></div>

2.Use the obtained buttons and numbered\_list values to enable product selection:

<div data-with-frame="true"><figure><img src="/files/KVphYtcwRilKCfazPJx0" alt=""><figcaption></figcaption></figure></div>

3.Use the list of possible values checker to validate the client’s input:

<div data-with-frame="true"><figure><img src="/files/jT3jRsCoiB1gAJ8k4v22" alt="" width="563"><figcaption></figcaption></figure></div>

4.Finally, display the price of the selected product to the client. This is convenient to do using the tools\_check\_user\_input() function

<div data-with-frame="true"><figure><img src="/files/Iq1PpKmeK3AX1MOiNjRJ" alt=""><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example to copy</summary>

```
list = [{"text":"T-shirts","price":100},{"text":"Shorts","price":150},{"text":"Socks","price":20},{"text":"Caps","price":50}]
res = tools_make_button_str_checker(list, "text", "2", "1")
numbered_list = res['numbered_list']
buttons = res['buttons']
checker = res['checker']

res_check = tools_check_user_input(list, user_input, 'text', 'price')
```

</details>

### Data selection from an array

<details>

<summary>Description</summary>

**array\_slice(array, start\_index, end\_index)**&#x20;

Parameters:

<mark style="color:red;">**!**</mark>**&#x20;array** - array \ <mark style="color:red;">**!**</mark>**&#x20;start\_index** - start of the slice \
**end\_index** - end of the slice (default: until the end)

</details>

<details>

<summary>Example</summary>

Let’s select a subarray starting from the 1st element:

<div data-with-frame="true"><figure><img src="/files/VcfdMiZ55J24JGFnmgHw" alt="" width="563"><figcaption><p>Example of using array_slice()</p></figcaption></figure></div>

&#x20;res will be \["Shorts", "Socks", "Caps"]&#x20;

Another example of selecting a subarray from the 0th to the 2nd element of the array:

<div data-with-frame="true"><figure><img src="/files/k43Fcg6W8y0q50YuEsuX" alt="" width="563"><figcaption><p>Example of using  array_slice()</p></figcaption></figure></div>

&#x20;res will be \["T-shirts", "Shorts"]

</details>

<details>

<summary>Code example to copy</summary>

```
list = ["T-shirts", "Shorts", "Socks", "Caps"] 
res = array_slice(list, 1)

res = array_slice(list, 0, 2)
```

</details>

### Unpacking array elements

<details>

<summary>Function</summary>

unpack\_list(array, var\_name) - this method iterates over an array and creates a separate variable for each element in the array with names like var1, var2, var3 and ect.&#x20;

<mark style="color:red;">**!**</mark> array - required parameter, an array of elements

var\_name - optional parameter, a string. If provided, it is used for naming the unpacked elements. Examples:

If var\_name is provided, the variable names are formed using var\_name

var\_name must follow variable naming rules.

</details>

<details>

<summary>Example</summary>

**Example 1:**

`array1 = ["one", "two", "three"]`

`ans1 = unpack_list(array1)`

**Result** - created deal variables:

`var1 = 'one'`

`var2 = 'two'`

`var3 = 'three'`

**Example 2:**

`array2 = ["one", "two", "three"]`

`var_name = 'custom'`

`ans2 = unpack_list(array2, var_name)`

**Result -** created deal variables:

`custom1 = 'one'`

`custom2 = 'two'`

`custom3 = 'three'`

</details>

### How to return a list without duplicate elements

<details>

<summary>Description</summary>

remove\_duplicates(array) - returns a list without duplicate elements.

<mark style="color:red;">**!**</mark> array - required parameter. The original list of elements with duplicates.

</details>

<details>

<summary>Example</summary>

Example:

arr = \[1, 2, 5, 1, 5, 3]

new\_arr = remove\_duplicates(arr)

Result - the list \[1, 2, 5, 3] will be assigned to the variable new\_arr.

</details>

### How to create dictionary

<details>

<summary>Description</summary>

Creating dictionary - declaring a dictionary

**name\_dictionary = {}**

</details>

### How to clear a dictionary

<details>

<summary>Description</summary>

Сlearing - is nothing more than declaring an empty dictionary.

**name\_dictionary = {}**

</details>

### How to get the dictionary value by key

<details>

<summary>Description</summary>

**name\[key] -** getting a dictionary element by key

</details>

<details>

<summary>Examples</summary>

**Example of working with a dictionary:**

&#x20;In this specific case, we’re accessing the value by the key "a". To retrieve a value from a dictionary using a specific key, use the following format: name\_dictionary\["key"]

<div data-with-frame="true"><figure><img src="/files/MWdkBYUnAdwBy0Ih5yPH" alt="" width="563"><figcaption></figcaption></figure></div>

Answer:

<div data-with-frame="true"><figure><img src="/files/j7qfB4wIyjrAhjSKYNjp" alt="" width="563"><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example to copy</summary>

```
dicts = {"a": "11", "d": "hi"}
/*getting from dictionary*/
aa = dicts["a"]
```

</details>

### How to get a list of keys from a dictionary

<details>

<summary>Description</summary>

**dict\_keys\_to\_array(data)** - to get a list of data dictionary keys

</details>

<details>

<summary>Examples</summary>

**Example:** Get a list of all dictionary keys

<div data-with-frame="true"><figure><img src="/files/LarjtirkfhoWasYGRMUa" alt="" width="563"><figcaption></figcaption></figure></div>

**Answer:**&#x20;

<div data-with-frame="true"><figure><img src="/files/oxRbBkF03M6f9JiPK1wM" alt=""><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example to copy</summary>

```
v={"A1":"orange","A2":"apricot","A3":"tangerine","A4":"apple","A5":"pear","A6":"kiwi","A7":"banana","A8":"peach"} 
key= dict_keys_to_array(v)
```

</details>

### How to get a list of values from a dictionary

<details>

<summary>Description</summary>

**dict\_values\_to\_array(data)** - to get a list of values from a dictionary

</details>

<details>

<summary>Examples</summary>

Example:

<div data-with-frame="true"><figure><img src="/files/t8iGmDNmhJJqNSgHTe6h" alt="" width="563"><figcaption></figcaption></figure></div>

Answer:

<div data-with-frame="true"><figure><img src="/files/BPHkuayNwsOQ6eiFc0Iq" alt="" width="375"><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example to copy</summary>

```
v={"A1":"orange","A2":"apricot","A3":"tangerine","A4":"apple","A5":"pear","A6":"kiwi","A7":"banana","A8":"peach"}
value= dict_values_to_array(v)
```

</details>

### How to get the values from the dictionary list by the specified key

<details>

<summary>Description</summary>

**get\_values\_by\_key(data, key)** - allows you to get values from the dictionary list by the specified key. Returns a list of values.

</details>

<details>

<summary>Example</summary>

Example: Get values from a list of dictionaries by key

<div data-with-frame="true"><figure><img src="/files/9w6Ic0hOcZi2gG04YscL" alt="" width="563"><figcaption></figcaption></figure></div>

Answer:

<div data-with-frame="true"><figure><img src="/files/Ev19e0ZbOf8qbT7IGgCC" alt="" width="563"><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example to copy</summary>

```
data = [{"Product": "Product№1", "Price": 1500}, {"Product": "Product№2", "Price": 6000}]
key = "Product"
result = get_values_by_key(data, key)
```

</details>

### How to replace a value in a dictionary&#x20;

<details>

<summary>Description</summary>

nam&#x65;**\['key'] = value** - replacing the dictionary element value by the specified key. If a non-existent key is specified, a new dictionary element will be added.

</details>

<details>

<summary>Description</summary>

**Example:**

To replace the value of a specific array element, write an assignment like arra&#x79;*\_name\[index] = value or dictionary\_name*\[key] = value

<div data-with-frame="true"><figure><img src="/files/HqMzVrxNnxxmwrdN2B9n" alt=""><figcaption></figcaption></figure></div>

Answer:&#x20;

<div data-with-frame="true"><figure><img src="/files/IaPQosSVCxMvYSud8xFX" alt="" width="563"><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example to copy</summary>

```
dicts = {"a": "11", "d": "hi"}

/*Replace in a dictionary*/
dicts['d'] = AAAAA

/*Add in a dictionary   dicts['key'] = 'VVVVVVVVVVV'*/
dicts['m']=new

/*Replace in a dictionary with a number*/
dicts['a'] = int('555')
```

</details>

### How to add a value to a dictionary

<details>

<summary>Description</summary>

**dictionary\_name*****\['key'] = 'value'** -* adding a new value to the dictionary.&#x20;

{% hint style="warning" %}
If the key did not exist before, a new key–value pair will be added; otherwise, the value for the specified key will be replaced.
{% endhint %}

</details>

<details>

<summary>Example</summary>

This is example the code in calculator field

<div data-with-frame="true"><figure><img src="/files/gz5Ksc46JKmyxws8736Z" alt=""><figcaption></figcaption></figure></div>

Answer

<div data-with-frame="true"><figure><img src="/files/8caN1w9Qujid697VnXt2" alt="" width="563"><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example to copy</summary>

```
/*adding a new value to the dictionary dicts['key'] = 'VVVVVVVVVVV'*/
dicts['m']=new
```

</details>

### How to check if a key exists in a dictionary

<details>

<summary>Description</summary>

**exist\_key(mass, key)** - to check if a key exists in a dictionary.

Parameters:&#x20;

**mass -** dictionary&#x20;

**key -** the key to search for

Returns True or False depending on whether the key was found.

</details>

<details>

<summary>Example</summary>

Example of usage:

<div data-with-frame="true"><figure><img src="/files/h81vJgsgmW4WWPkxoGDb" alt=""><figcaption></figcaption></figure></div>

<div data-with-frame="true"><figure><img src="/files/9gfRggZjJNr0u1v1vTPB" alt="" width="563"><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example to copy</summary>

```
s = {"1": 123, "2": 234, "q": {"w": "e"}} 
q = if(exist_key(s, 'q'), 'Found', 'Another line')
```

</details>

### How to check the position of a key in a dictionary

<details>

<summary>Description</summary>

**key\_index(mass, key)** - to check the position of a key in a dictionary.

**Parameters:** \
**mass - dictionary**  \
**key -** Key to search for.

{% hint style="info" %}
The position in a dictionary is counted from 0. Thus, the first element is 0, the second element is 1, and so on.
{% endhint %}

</details>

<details>

<summary>Example</summary>

Example of usage:

<div data-with-frame="true"><figure><img src="/files/JmCRfyDDeRiP1XIQlUih" alt=""><figcaption></figcaption></figure></div>

*Result:*

<div data-with-frame="true"><figure><img src="/files/nLcqquOEe5yMmymhmWoh" alt="" width="375"><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example to copy</summary>

```
s = {"1": 123, "2": 234, "q": {"w": "e"}} 
q = key_index(s, 'q')
```

</details>

### How to get the number of elements in a dictionary

<details>

<summary>Description</summary>

**arr\_len(mass)** - to determine the length of a dictionary.&#x20;

Parameter:\
**mass -** *dictionary*&#x20;

Result: Returns a number – the length of the dictionary.

{% hint style="warning" %}
Будьте внимательны When passing a parameter to the function: if the function is called without parameters, it returns 0; if the parameter is not a list or a dictionary, it returns -1.
{% endhint %}

</details>

<details>

<summary>Example</summary>

Example of usage:

<div data-with-frame="true"><figure><img src="/files/tDFaZuqwzHXOmrVUo12L" alt=""><figcaption></figcaption></figure></div>

Answer:

<div data-with-frame="true"><figure><img src="/files/n5FzicyVtTB6sJOcT2F5" alt="" width="375"><figcaption></figcaption></figure></div>

</details>

### How to delete an element from a dictionary

<details>

<summary>Description</summary>

By index or key

**del(mass, key)** - to delete an element from a list by index or from a dictionary by key.\
It takes two parameters: the list/dictionary and the index/key to delete.\
Returns the modified dictionary or list without changing the original object.

{% hint style="warning" %}
If the values in a list or dictionary are numbers, use the remove() function to delete an element.
{% endhint %}

</details>

<details>

<summary>Example</summary>

Example with a dictionary

<div data-with-frame="true"><figure><img src="/files/7n3M90a136XpPPqt93CA" alt=""><figcaption></figcaption></figure></div>

Result

<div data-with-frame="true"><figure><img src="/files/sXxdZKCeTdCfyRBl33pw" alt="" width="563"><figcaption></figcaption></figure></div>

Example with an array

<div data-with-frame="true"><figure><img src="/files/W7C0tLQMSY2kLLqPKOd7" alt="" width="563"><figcaption></figcaption></figure></div>

Result

<div data-with-frame="true"><figure><img src="/files/A23Gima94dlCrxB1CNiz" alt="" width="563"><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example to copy</summary>

Example with a dictionary

`s={"1":123, "2":234, "q":{"w":"e"}}`<br>

`q=del(s,'q')`

Example with an array

`s=["John", "Ann", "Sophie"]`\ <br>

`q=del(s, 1)`

</details>

### How to convert a dictionary into a human-readable text

<details>

<summary>Description</summary>

**humanize(dict, delimiter, from\_i, to\_i)**&#x20;

Parameters:

**dict** - dictionary name\
**delimiter** - delimiter between lines\
**from\_i** - index of the element to start output from (0-based)\
**to\_i** - index of the element to end output at (not inclusive)

</details>

<details>

<summary>Example</summary>

Let's look at the example:

<div data-with-frame="true"><figure><img src="/files/PBh5rc7iDlllC4Su6WNc" alt="" width="563"><figcaption></figcaption></figure></div>

Answer

<div data-with-frame="true"><figure><img src="/files/aCe6qOCii1aIJWYFnJ6D" alt="" width="563"><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example to copy</summary>

```
dict = {"[id146467928|Alex]":"6","[id145255525|Маx]":"20"}
r = humanize(dict, ': ')
```

</details>

### For sorting dictionaries&#x20;

sort() | sort\_by\_value()

<details>

<summary>Description</summary>

<mark style="background-color:blue;">**Dictionary sorting**</mark>

**sort(dict, b)** - Sorts a list by value, and a dictionary by key

Parameters:

<mark style="color:red;">**!**</mark>**&#x20;dict**- dictionary

**b** - sorting direction (False – ascending by default, True – descending)

<mark style="background-color:blue;">**Dictionary sorting by value**</mark>

**sort\_by\_value(dict, b) -** Sorting a dictionary by values.

Parameters:

<mark style="color:red;">**!**</mark>**&#x20;dict**- dictionary

**b** - sorting direction (False – ascending by default, True – descending)

</details>

<details>

<summary>Examples</summary>

Example: sorting a list in descending order and a dictionary in ascending order:

<div data-with-frame="true"><figure><img src="/files/HYWBctPNYE21Cewsgzno" alt=""><figcaption></figcaption></figure></div>

<div data-with-frame="true"><figure><img src="/files/3AVpOoQMVfHIvUlHoi5T" alt="" width="375"><figcaption></figcaption></figure></div>

Sorting a dictionary by its values:

<div data-with-frame="true"><figure><img src="/files/JSrMKEYaLlsboqUbSnij" alt=""><figcaption></figcaption></figure></div>

<div data-with-frame="true"><figure><img src="/files/vlUNjwY1Ecr1VmmZcIqQ" alt="" width="375"><figcaption></figcaption></figure></div>

</details>

<details>

<summary>Code example to copy</summary>

Example: sorting a list in descending order and a dictionary in ascending order:

`array1=[5,4,0,6,3,0]`<br>

`array1=sort(array1, True)`

`dict={"Ann":5, "John":4, "Sophie":0, "Alex":6, "Kate":3, "Harry":0}`

`dict=sort(dict)`

Sorting a dictionary by its values:

`dict={"Ann":5, "John":4, "Sophie":0, "Alex":6, "Kate":3, "Harry":0}`&#x20;

`dict=sort_by_value(dict)`

</details>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mavibot.ai/chatbot/functions/calculator/arrays.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
