Working with Arrays and Dictionaries

Work in Salebot with data types like dictionary and array

An array is a data structure that stores a set of values (array elements) that are identified by the index. Set to []. The record of the form q = [] means that the variable q contains an array without elements. If q = [2,5,7,4,9] - then in our array there are 5 elements - this is the length of the array. The indexes of the elements, their address in the array, start with 0. Thus the index of the element "2" will be 0, and the index of the element "4" in our array will be 3.

A dictionary is a data structure that represents a specially organized set of stored data elements. All data is stored as key-value pairs. Data items are accessed by key. The key must always be unique within the same dictionary, the data can be duplicated if necessary.

The record of the form s = {} means that the variable s contains a dictionary that does not contain elements. We can put a key-value pair, or even several: s = {"key 1":"value 1", "key 2":"value","key 3:"value"} Each key and value are quoted, a colon is placed between the key and value, and the key-value pairs are separated by a comma.

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

rainbow = [red, orange, yellow, green, blue, blue, purple] is an array.

If we record

rainbow = {"each":"red","hunter":"orange","wishes":" yellow","know":"green","where":"blue","sits":"blue","pheasant":"purple"}

we’ll get a dictionary. If we look closely at the rainbow, for example, we notice that blue is not uniform, but consists of turquoise, light blue and dark blue. Let’s write down: blue = [turquoise, light blue, dark blue]. And now insert our array "blue" into the dictionary "rainbow":

rainbow = {"each":"red","hunter":"orange","wishes":" yellow", "know":"green","where": [turquoise, light blue, dark blue],"sits":"pheasant":"purple"}

How do we then specify the address of the element, let’s say "turquoise"? In dictionaries, addressing goes by the key. So, the address of the array [turquoise, light blue, dark blue] will be the key "where". And in the array the address is the index, so the element "turquoise" address will be 0. Collect all together, separating the keys by vertical line: where|0 is the address of the element "turquoise"

How to Work with Addressing (JSON)

When we receive answers from third-party services about the API, most often we get the JSON, which is a dictionary. It is often a problem to save a value from this dictionary into a variable.

It is better to understand the principle of addressing when working with JSON on the example. {"suggestions": [{"value": "Belarusian ruble", "unrestricted_value": "Belarusian ruble", "data": {"code": "933", "strcode": "BYN", "Belarusian ruble", "country": "Belarus"}}}}

Here we have a dictionary containing one key-value suggestions - key for array [{"value": "Belarusian ruble", "unrestricted_value": "Belarusian ruble", "data": {"code": "933", "strcode": "BYN", "Belarusian ruble", "Country}:"]}

In the key suggestion value there is an array having only one element - a dictionary: {"value": "Belarusian ruble", "unrestricted_value": "Belarusian ruble", "data": {"code": "933", "strcode": "BYN": "Belarusian ruble", "Country": "}} suggestions|0 - the key for the first (and only) element of the array {"value": "Belarusian ruble", "unrestricted_value": "Belarusian ruble", "data": {"code": "933", "strc": "BYODEN", "Belarusian ruble", ""Country}:"

suggestions|0|value - key for "Belarusian ruble" value

{"suggestions": [{"value": "Belarusian ruble", "unrestricted_value": "Belarusian ruble", "data": {"code": "933", "strcode": "BYN", "Belarusian ruble", "country": "Belarus"}}}}

The longest key in this JSON: suggestions|0|data|strcode - the key for the BYN value

{"suggestions": [{"value": "Belarusian ruble", "unrestricted_value": "Belarusian ruble", "data": {"code": "933", "strcode": "BYN", "Belarusian ruble", "country": "Belarus"}}}}

The keys are separated by a vertical line. If JSON has an array, then access to its element goes by number, starting from 0, and is also written through a vertical line. Array numbering comes with 0

In addressing, numbers are equated with strings and vice versa. That is, they are equal to '6' and '6'.

In addition to API responses, arrays and dictionaries can be used for easy data storage.

For example, you need to limit the number of people who have access to a specific section of the bot. You can save the list of their ID to an array by putting it into a general variable of the project, and in the conditions of blocks or arrows check the user id entry into that array.

Another example is when you need to save, for example, the number of points of each player in a team game. To do this, you can use a dictionary with the user ID keys and the values with their points.

At times there is a task to change any array or dictionary. The functions described in this article are created for this purpose. Most of the methods described below work with arrays and dictionaries.

How to get an array or dictionary element

get(mass, key, b) - to get an array element or dictionary. 3 parameters. Dictionary or array and search key are required parameters. The third parameter is optional. If you pass False, the vertical line in the key is considered as part of the key, not a separator. Returns the value found.

If the key is not found, None will return

If you want to use a vertical line in the key, use the third option False.

Example with dictionary:

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

Example with an array:

How to write a value to an array or dictionary

set(mass, key, value) - to create a new array or dictionary with a changed value. Input has 3 parameters. Dictionary(array), key(index) and write value are required. The method returns a modified string.

The method does not change the original variable!

Example with dictionary:

This code replaces the value of q. The result is: {"1":123,"2":234,"q":"text"}

Example with an array:

The default data is inserted as strings, and if you want to insert an array or dictionary, pass the optional True parameter. It means that you insert the JSON.

If you want to use a vertical line in the key, then the fourth parameter False.The third parameter can pass the default value to False.

How to Verify Key Availability

exist_key(mass, key) - to check the presence of a key in the dictionary. Two parameters: dictionary and search key. The return value is True or False, depending on whether the key is found or not.

Example of use:

s = {"1": 123, "2": 234, "q": {"w": "e"}}

q = if(exist_key(s, 'q'), 'Found', 'More string')

How to check the position of the key in the dictionary

key_index(mass, key) - to check the position of the key in the dictionary. Two parameters: dictionary and search key.

The position in the dictionary is counted with 0. So the first element is 0, the second element is 1 and so on.

Example of use

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

Result is q = 2

How to Check the Presence of an Element in an Array

in_array(mass, value) - to check if an element is in the array. For the parameter: array and value for search.

The True or False return value was found or not.

Example of use:

s = ["John", "Peter", "Jack"]

q = if(in_array(s, 'Ann'), 'Found', 'More string')

How to find out the length of an array or the number of elements in a dictionary

arr_len(mass) - to determine the length of the array or how many values in the dictionary. Accepts one parameter - a dictionary or an array. Returns a number.

Be careful when passing the parameter to a function! If you call a function without parameters, it will return 0 if the parameter does not have an array, will return 1.

Example use array:

s = ["John", "Peter", "Jack"]

q = if(arr_len(s) > 5, 'The group is complete', 'Join us!')

dictionary:

s = {"John": 4, "Olivia": 3, "Ann": 15}

i = arr_len(s)

How to insert an element at the end of an array

append(mass, element) - to insert an element at the end of the array. Two parameters: array and insertable element. Returns the array to which the value is added at the end. The original string is not changed.

Example of use:

s = ["Nick", "Peter", "Mary"]

q = append(s, 'Bob')

In the variable s, the result will be ["Nick", "Peter", "Mary"], and in q ["Nick", "Peter", "Mary", "Bob"]

The default data is inserted as strings, and if you want to insert an array or dictionary, pass an additional True parameter. It means that you insert the JSON.

How to Insert a Value into a Specific Array Position

insert(mass, index, value) - to insert an element into a certain position of the array. Three parameters: array, insert position, value. Returns the array in which the value is added to the specified position. The original string does not change.

s = ["Vasya", "Petya", "Lena"]

q = insert(s, 1, 'Nikita')

In the variable s, the result will be ["Vasya", "Petya", "Lena"], and in q ["Vasya", "Nikita", "Petya", "Lena"]

The default data is inserted as strings, and if you want to insert an array or dictionary, pass an additional True parameter. It means that you insert the JSON.

How to remove an element from an array or dictionary

By index or key

del(mass, key) - to remove an element from an array or dictionary by index. Accepts two parameters: an array or a dictionary, the key by which it will be deleted. Returns a modified dictionary or array, the original string does not change.

Example with dictionary:

By value

remove(mass, value) - to remove a value from an array. Accepts two parameters: the array and the value to be removed from the array. Returns the changed array, the original string does not change.

How to find out the position of an element in an array

index(mass, value) - to determine the position of the element in the array. Accepts two parameters: array and value, the position of which should be deduced. If the element is not in the array, returns -1.

How to Translate a Dictionary into Man-Readable Text

humanize(s, delimiter=' ', from_i=0, to_i=9999) - to translate from a dictionary into humanreadable text. Each pair of key value with a new string. Separated by a space or a sign that you pass with the second parameter. The first parameter is json.

If you need to display the part on the screen, additionally pass the index of the starting element and to which output (not included). The numbering starts from scratch.

Example:

dict = {"[id146467928|David]":"6"}

r = humanize(dict, ': ')

in the variable dict will be [id146467928|David]: 6

How to Exclude One Array from Another

except_arr(s1, s 2) - To exclude one array from another. Accepts 2 parameters, where s1 array from where to exclude, s2 array elements which exclude

Example:

s1 = [1, 2, 3]

s2 = [2, 3]

s3 = except_arr(s1, s2)

In the s3 variable will be [1]

Last updated