UiPath

Array manipulation with UiPath (List, Dictionary, Array)

In UiPath Studio development, there are times when you want to use an array of multiple variables in a single variable.

This article explains the difference between Array, List, and Dictionary arrays, and how to use them.

 

 

 Related Articles Create a practical UiPath robot with Udemy’s online course

 

The operator of this blog, F-penIT blog

 

This site was created by translating a blog created in Japanese into English using the DeepL translation.

Please forgive me if some of the English text is a little strange

 

Difference between Array, List, and Dictionary

Both Array and List can have multiple elements, and the elements are specified numerically.

The difference between Array and List is that Array does not allow you to add elements, while List allows you to add elements.

Dicitonary differs from Array and List in that the elements are specified by keys.

Number of elements The value of the element How to retrieve the data
Array fixed Changeable numeric value specification
List variable Changeable numeric value specification
Dictionary variable Changeable Key designation

 

F-pen
F-pen
Array does not allow adding elements, so List is easier to use when retrieving data by specifying numbers.
sea otter
sea otter
If you want to retrieve data by specifying a key, use a Dictionary.

 

Array

Array is an array with a fixed number of elements, changeable element values, and a numeric data retrieval method.

To create an Array variable

To create an Array variable
①To create an Array variable, click on “Array Of [T]” for the variable type.

②Select the variable T in the array, and click [OK].

③Make sure that the Array variable specified as the variable type is displayed.。

 

Output each element of an Array to the log

To log each element of an Array, you can use the “For Each” activity to extract each element, or you can specify the number of the Array variable.

Sample Process
The elements in the variable Array are iterated and numbered, and the values in the elements are output to the log.

sea otter
sea otter
The array numbering starts from 0, so the first element should be 0, the second element should be 1, and the third element should be 2.

 

・Text for each value in the process

{"apple","orange","banana"}
"repeat: "+item.ToString
"Numbering(0):"+arr(0)
"Numbering(1):"+arr(1)
"Numbering(2):"+arr(2)

 

・Assign  Properties

 

 

・For Each  Properties

F-pen
F-pen
The “TypeArgument” specifies the type of the variable specified in the array.

 

・Variables

・Execution result  Log

 

List

List is an array with a variable number of elements, changeable element values, and a numeric data retrieval method.

Procedure for creating a List variable

Procedure for creating a List variable
①Under Variable Types, click Browse for Types.

②”System.Collections.Generic.List” in the search window.

③Select [List<T>], select the variables to be specified in the array in the upper right corner, and click [OK].

④Make sure List<selected variable> is displayed for the variable type.

 

Output each element of the List to the log.

The log output for each element of the List can be done by extracting each element with the “For Each” activity, or by specifying the number of the List variable.

 

Sample Process
Repeats and numbers the elements in the variable List, and outputs the values in the elements to the log.

 

・Text for each value in the process

New List(of String) From{"apple", "orange","banana"}
"repeat: "+item.ToString
"Numbering(0):"+list(0)
"Numbering(1):"+list(1)
"Numbering(2):"+list(2)

 

・Assign  Properties

 

F-pen
F-pen
The List type cannot add elements without initialization, so initialize it with New List(of String).
sea otter
sea otter
By adding From{} after New List(of String), you can set the initial value.

 

・For Each  Properties

F-pen
F-pen
The “TypeArgument” specifies the type of the variable specified in the array.

 

・Variables

・Execution result  Log

 

Adding elements to a List (Add To Collection)

To add an element to the list, use the “Add To Collection” activity.

“Add To Collection”は、Programming > Collection にあります。

 

Sample Process
Add an element to the variable List and output the value in the element to the log.

 

・Text for each value in the process

New List(of String) From{"apple", "orange","banana"}
"repeat: "+item.ToString

 

 

・Assign Properties

 

・Add To Collection  Properties

・For Each  Properties

・Variables

・Execution result  Log

 

 

Remove an element from the List(Remove From Collection)

To remove an element from the list, use the “Remove From Collection” activity.

“Remove From Collection” is located in Programming > Collection.

 

Sample Process
Delete one element of the list and output the value in the element to the log.

・Assign  Properties

・Remove From Collection  Properties

F-pen
F-pen
True or false will be returned in “Result” depending on whether or not the value of the item could be removed from the collection.

 

・For Each  Properties

・Variables

・Execution result  Log

sea otter
sea otter
Since “apple” has been removed from the target list, the search result will be True, and “apple” has been removed from the list.

 

Clear the elements in the List(Clear Collection)

To clear all the elements of a list, use the “Clear Collection” activity.

“Clear Collection” is located in Programming > Collection.

 

Sample Process
Clear all elements of the list and output the values in the elements to the log.

 

・Assign  Properites

・Clear Collection  Properties

・For Each  Properties

・Variables

・Execution result  Log

F-pen
F-pen
Since all the elements in the list have been deleted, the log in “For Each” will not be printed.

 

Replace an element in the List(Assign)

To replace an element in the List, use the “Assign” activity.

“Assign” is located in Workflow > Control.

 

Sample Process
Replace one of the elements of the List with another value, and output the value in the element to the log.

sea otter
sea otter
“Assign Replace element 2” replaces the second element in the list with “pear”.

 

・Assign list initialization  Properties

・Assign Replace element 2  Properties

・For Each  Properties

・Variables

・Execution result  Log

F-pen
F-pen
In the third “For Each” (the second element), the replaced “pear” is output.

 

Whether or not the element is in the List(Exists In Collection)

The “Exists In Collection” activity is used to check for the presence of a given element in a list.

“Exists In Collection” is located in Programming > Collection.

 

Sample Process
Checks for the presence of the specified element in the list and outputs the result to the log.

・Assign  Properties

・Exists In Collection  Properties

・Variables

・Execution result  Log

sea otter
sea otter
Since there are no “melon” elements in the list, false will be returned.

 

Number of elements in the List(.Count)

For the number of elements in the List, use the Count of the List variable.

 

Sample Process
Count the number of elements in the list and output to the log.

 

・Text for each value in the process

New List(of String) From{"apple", "orange","banana"}
" list Number of elements: "+list.Count.ToString

 

F-pen
F-pen
Count after the list variable outputs the number of elements in the list as an int type, and .ToString converts the int type to a String type.

 

・Assign  Properties

・Variables

・Execution result  Log

sea otter
sea otter
You can see that the number of elements in the list is 3.

 

Dictionary

A Dictionary is an array with a variable number of elements, changeable element values, and a Key-specified way to retrieve data.

While Array and List specify elements by numbers, Dictionary specifies elements by “keys”.

Procedure for creating a Dictionary variable

Procedure for creating a Dictionary variable
①Under Variable types, click Browser for types.

②”System.Collections.Generic.Dictionary” in the search window.

③Select [Dictionary<TKey><TValue>], select the variable types of Key and Value in the upper right pull-down, and click [OK] button.

④Make sure [Dictionary<selected Key variable type><selected Value variable type>] is displayed.

 

Output each element of the Dictionary to the log

To log each element of the dictionary, you can either extract each element with a “For Each” activity or specify a List variable with a Key.

Sample Process
It repeats the elements in the variable Dictionary and outputs the values in the elements to the log by specifying Key.

 

・Text for each value in the process

New Dictionary(of String, Integer) From{{"apple", 100},{"orange", 200},{"banana", 300}}
"apple value: "+dic("apple").ToString
"orange value: "+dic("orange").ToString
"banana: "+dic("banana").ToString
"repeat: "+item+" "+dic(item).ToString

 

F-pen
F-pen
By setting the value of “For Each” to dic.key, the key values in the dic are set to items in order.
sea otter
sea otter
Since item contains a Key value, dic(item) will output the Value value corresponding to the Key value.

 

・Assign

・For Each

 

・Variables

・Execution result  Log

 

 

Add an element to Dictionary(Assign)

To add elements to the dictionary, use the “Assign” activity.

“Assign” is located in Workflow > Control.

 

Sample Process
Add an element to the Dictionary and output the value in the element to the log.

 

F-pen
F-pen
[Assign dic add] adds an element to the dic by specifying a Key value that does not exist in the dic on the left side and a Value value on the right side.

 

・Text for each value in the process

New Dictionary(of String, Integer) From{{"apple", 100},{"orange", 200},{"banana", 300}}
item+":"+dic(item).ToString

 

 

・Assign dic initialization  Properties

・For Each  Properties

・Variables

・Execution result  Log

 

Remove an element from a dictionary (InvokeMethod )

Use the [InvokeMethod] activity to delete an element from the Dictionary.

[InvokeMethod] is located in Programming > Excure.

 

Sample Process
Delete one element of the Dictionary and output the value in the element to the log.

・Assign Properties

 

・InvokeMethod  Properties

sea otter
sea otter
For [Method Name], I’ve specified Remove to delete the data in the Dictionary.

 

・Value of the “Parameter” in the “Method Name”.

 

・For Each  Properties

・Variables

・Execution result  Log

F-pen
F-pen
Since the key value “apple” has been removed from the dic, only Orange and banana will be output.

 

 

Replace an element in a dictionary (Assign)

To replace an element in the Dictionary, use the “Assign” activity.

“Assign” is located in Workflow > Control.

 

Sample Process
Replace one of the elements of the Dictionary with another value, and output the value in the element to the log.

 

・Assign Initialization  Properties

 

・Assign Replace  Properties

F-pen
F-pen
The Key value “apple” in the dic is specified on the left side, and the changed Value value is specified on the right side.

 

・For Each  Properties

・Variables

・Execution result  Log

sea otter
sea otter
The value of “apple” has been replaced by 50 instead of 100.

 

Number of elements in the Dictionary(.Count)

The number of elements in the Dictionary uses the Count of the Dictionary variable.

 

Sample Process
Counts the number of elements in the Dictionary and outputs it to the log.

・Assign  Properties

 

・LogMessage

・Variavbles

・Execution result  Log

 

Summary

  • Array is an array with a fixed number of elements, changeable element values, and a numeric data retrieval method.
  • List is an array with a variable number of elements, changeable element values, and a numeric data retrieval method.
  • A Dictionary is an array with a variable number of elements, changeable element values, and a key-specified data retrieval method.
  • List is easier to use than Array because the number of elements is variable.
  • If you want to specify the key, use Dictionary.

 

Back to Table of Contents

 

 Related Articles Create a practical UiPath robot with Udemy’s online course

 同カテゴリ UiPath