Categories
RPA UiPath

Data Manipulation with Lists and Dictionaries in Studio (UiRPA part 9)

Data Manipulation with Lists and Dictionaries in Studio

About the course

In this course, we will cover the most important data manipulation operations and examples. These will be demonstrated using some of the most common data
types that you will encounter in your work, namely Lists and Dictionaries.

What you will learn in this course

At the end of this course you will be able to perform the following operations in UiPath Studio:

  • Use specific methods to initialize and manipulate List variables.
  • Use specific methods to initialize and manipulate Dictionary variables.

The basic concepts of Lists

Lists (or List<T>, as you will encounter them) are data structures consisting of objects of the same data type (for example string or integer). Each object has a fixed position in the list; thus, it can be accessed by index. While arrays are fixed-size structures for storing multiple objects, lists allow us to add, insert and remove items.

Lists can store large numbers of elements – names, numbers, time coordinates and many others. Lists provide specific methods of manipulation, such as:

  • Adding and removing items.
  • Searching for an element.
  • Looping through the items (and performing certain actions on each).
  • Sorting the objects.
  • Extracting items and converting them to other data types.

What are some business scenarios in which you will most likely encounter lists?

  • Storing the computer names of the members of a project team for a certain configuration that needs to be made.
  • Collecting and storing the numbers of invoices that meet certain criteria.
  • Keeping track of the ticket numbers created in a certain period on a certain issue.

UiPath methods applicable to collections

Add to Collection: Adds an item to a specified collection. It is equivalent to List.Add(). It can be used, for example, to add a new name to a list of company names.

Remove from Collection: Removes an item from a specified collection and can output a Boolean variable that confirms the success of the removal operation. This activity can be used, for example, to remove an invoice number from a list of invoices to be processed.

Exists in Collection: Indicates whether a given item is present in a given collection by giving a Boolean output as the result. We can use this activity to check whether a list of clients contains a specific name.

Clear Collection: Clears a specified collection of all items. One possible use is to empty a collection before starting a new phase of a process that will populate it again.

Dictionaries

Dictionaries (or Dictionary<TKey, TValue>, as you will encounter them) are collections of (key, value) pairs, in which the keys are unique. Think of the Address Book in your mobile phone, where each name has corresponding data (phone number(s), email).

The data types for both keys and values have to be chosen when the variable is declared. Data types in Dictionaries can be any of the supported variables (including Dictionaries, for example).

The operations that are most often associated with Dictionaries are:

  • Adding and deleting (key, value) pairs.
  • Retrieving the value associated with a key.
  • Re-assigning new values to existing keys.

What are some business scenarios in which you will most likely encounter dictionaries?

  • Storing configuration details or other information that needs to be accessed throughout a process.
  • Storing the job titles or other relevant information of employees.
  • Storing the bank accounts of suppliers.

Methods for working with Dictionaries

Initialisation: Just like in the example of Lists, Dictionary variables need to be initialized with instantiated objects. In the previous example, the instantiation and initialization were done inside an ‘Assign’ activity. However, as you may remember from the Lists chapter, they can also be done from the Variables panel. 

Adding Key-Value Pairs: VarName.Add(Key, Value) – adds an item to an existing Dictionary. Because Add does not return a value, we use the Invoke Method activity.

Removing Keys: VarName.Remove(Key) – removes an item from the Dictionary. It can be used in an ‘Assign’ activity.

Retrieving:

  • VarName.Item(Key) – returns the Dictionary item by its key.
  • VarName.Count – returns an Int32 value of the number of Dictionary items.
  • VarName.ContainsKey(Key) – checks if the item with the given key exists in the Dictionary and returns a Boolean result.
  • VarName.TryGetValue(Key, Value) – checks if an item with a given key exists in the Dictionary and returns a Boolean result and the value if found.

Practical ideas

How to display the content of a list using the Log Message activity: String.Join(“, “, ListOfItems)

How to merge two lists: Enumerable.Concat(ListOfItems.AsEnumerable, ListOfItems2.AsEnumerable).ToList

How to define and initialise an empty List in the Variable panel: new List(of String). And with values: new List(of String) from {“red”, “green”, “blue”}

The variable type used for lists is System.Collections.Generic.List of Type. “Of Type” defines the type of elements contained within the list.

To sort lists, we can use the Invoke Method activity and invoke the instance method Sort.

How to define and initialise an empty Dictionary: new Dictionary(of Int32, String)

To declare a dictionary, we need to create a variable with System.Collections.Generic.Dictionary as type. Then we select its key and value data types.

The number of elements (Count)

For an Array: Array.Count

For a List: List.Count

For a Dictionary: Dictionary.Count

Facts

List Manipulation includes:

  • Adding and removing items.
  • Searching for an element.
  • Extracting items and converting them to other data types.
  • Looping through the items.
  • Sorting the objects.

The default initialization value of an integer is 0.

Congratulation

Diploma of completion
Diploma of completion