What is a Dictionary?

You are currently viewing What is a Dictionary?

Overview

A Dictionary is an unordered collection of objects which can be accessed by a key. A dictionary uses a key, rather than a numerical index, to store these objects. Every Value has a key associated to it and are stored as Key:Value pairs. 

The best example of a dictionary in programming is the English Dictionary. Just like an English Dictionary, all words and their corresponding definitions are stored in the same format:

Word Definition
Apple
The usually round, red or yellow, edible fruit of a small tree.
Application
An act of applying.
Apposable
Capable of being apposed, or applied one to another.

A dictionary in programming looks pretty similar:

Below are the key characteristics of a dictionary:

  • Variable Size – The size of a dictionary is not fixed and elements can be added or removed at any time during an automation.
  • Unordered Values – There is a no specific order to the elements in a dictionary. Elements can change places at any time and are not accessed by their location in the dictionary.
  • Same Data Type – All elements must be the same data type. A dictionary of strings can only hold string variables.
  • Accessed by Key, Not Index– Each element in a dictionary is given a label it can be referenced by, called a key. You can use the key to access a specific element in the dictionary.
  • No Duplicates – All key names must be distinct. On the other hand, there can be 2 distinct keys with the same value.

How to Access Values in a Dictionary?

You can access a value within a dictionary by referring to its key. In UiPath, this is accomplished by writing out the dictionary name followed by the key surrounded by parentheses.

As the result of this action is an object, it can then be converted into any data type. For the below example, we can convert the object into a string using the ToString method.

We can also convert the object into an Integer with the CInt Function.

For more information on converting variables to different data types, click here.

How to Replace a Dictionary Value?

Dictionary values can be replaced any time during an automation using the key assigned to it. This can be accomplished in UiPath by using the Assign activity as shown below:

How to add Elements to a Dictionary?

As dictionaries do not have a fixed size, additional elements can be added any point during an automation using the Add Method. Using this method, you must include the key name and value you wish to add to the dictionary and both must be matching data types. An example is shown below:

Elements can also be added to dictionaries by using the assign activity as shown below:

How to Remove Elements from a Dictionary?

In addition to adding elements to a dictionary, elements can also be removed at any point during an automation using the Remove Method. Using this method, you only need to know the Key you wish to remove. An example of the Remove Method is shown below:

What can you do with the Dictionaries?

Below are some great examples of the use of Dictionaries:

  • Storing Data From a Config File

Commonly Used Properties

The following are some commonly used properties for Dictionaries. A property is a characteristic or attribute of an object (i.e. Dictionary). To learn more about properties, click here

Count - Gets the number of Key/Value pairs.

Example Property Example Return Return Type
Dictionary.Count
40
Integer (Int32)

Commonly Used Methods

The following are some commonly used methods for Dictionaries. Methods are actions that objects (i.e. Dictionary) can perform. To learn more about methods, click here.

Add(TKey,TValue) - Adds the specified key and value to the dictionary.

Example Method Example Return Return Type
Dictionary.Add("Shape","Round")
N/A - No Return
N/A - No Return

Clear - Removes all keys and values from the dictionary.

Example Method Example Return Return Type
Dictionary.Clear
{}
Dictionary

ContainsKey(TKey) - Determines if the dictionary contains the specified key.

Example Method Example Return Return Type
Dictionary.ContainsKey("Color")
True
Boolean

ContainsValue(TValue) - Determines if the dictionary contains the specified value.

Example Method Example Return Return Type
Dictionary.ContainsValue("Red")
True
Boolean

Remove(TKey) - Removes the value with the specified key.

Example Method Example Return Return Type
Dictionary.Remove("Color")
N/A - No Return
N/A - No Return