What is a List?

You are currently viewing What is a List?

Overview

A list is an ordered collection of elements of the same data type with no fixed size. In other words, a list is a way to store multiple values in a single variable, instead of declaring a separate variable for each value. Lists are commonly used in programming to organize data so that a related set of values can be stored together and be easily sorted or searched. The main reason to use a list over a variable is that lists have the ability to add or remove elements from your list, whereas an array you can’t.

Below are the key characteristics of an array:

  • Variable Size – The size of the List is not fixed and elements can be added or removed at any time.
  • Ordered Values – There is a specific order to a List. Elements don’t swap places within the List.
  • Same Data Type – All elements must be the same data type. An List of strings can only hold string variables.
  • Indexed – Each element in a List is given a number, called an index, starting at 0. You can use the index to access a specific element in the List based on their index number.

How to Create a List in UiPath?

In UiPath, a List, just like an array, is seen as a set of curly brackets containing variables of the same data type separated by commas, for example:

How to Access Elements in a List?

 You can access an element within a list by referring to its index number. In UiPath, this is accomplished by writing out the list name followed by the index number surrounded by parentheses. In the below example, we are looking to obtain the 3rd element in the list and assign it to a string variable.

How to Change the Value of a List Element?

Elements within the list can be changed at any time during an automation. Changing the value of a list element can be accomplished using the Assign activity in UiPath as shown below:

How to Add Elements to a List?

As lists do not have a fixed size, additional elements can be added any point during an automation. This can be accomplished with the Add to Collection activity as shown below which adds an object to the end of the list.

To use the Add to Collection activity, you will need to input the Collection (i.e. the List variable), the item you wish to add to the list, and the TypeArgument (the data type that is being stored in the list). Below is an example of the properties pane with the data filled in:

How to Remove Elements to a List?

In addition to adding elements to a list, you can remove them as well at any point during an automation. There are various ways of removing elements from a list. 

The first method is using the Remove Method inside of an Invoke Method activity which removes the first occurrence of a specified object in the list. Note that if there are two or more of the same object in a list, only the first instance will be removed. For example, using the Remove method to remove a 3 on a list of {1,2,3,3} will just remove the first 3, returning {1,2,3}. A visual example of the Remove Method is shown below:

The parameters of the Remove Method are shown below:

The second method to remove an object from a list is using the RemoveAt(Index) Method inside an Invoke Method activity which removes the element at a specified index from the list. For example:

The parameters of the RemoveAt Method are shown below:

What can you do with Lists?

Below are some great examples of the use of Lists:

  • Contain a collection of invoice numbers
  • Contain a customer list
  • Etc.

Commonly Used Properties

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

Count - Gets the number of elements in the list.

Example Property Example Return Return Type
{4,3,5}.Count
3
Integer (Int32)

Commonly Used Methods

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

Add(Object) - Adds an object to the end of the list.

Example Method Example Return Return Type
{11,22,33}.Add(44)
{11,22,33,44}
List

Clear - Removes all elements from the list.

Example Method Example Return Return Type
{11,22,33}.Clear
{}
List

Contains(Object) - Determines whether an element is in the list.

Example Method Example Return Return Type
{11,22,33}.Contains(33)
True
Boolean

IndexOf(Object) - Returns the index of the first occurrence of a specified value in the list.

Example Method Example Return Return Type
{11,22,33}.IndexOf(33)
2
Integer (Int32)

Insert(Integer,Object) - Inserts and object into a list at a specified index.

Example Method Example Return Return Type
{11,22,33}.Insert(2,44)
{11,22,44,33}
List

LastIndexOf(Object) - Returns the index of the last occurrence of a value in the list.

Example Method Example Return Return Type
{11,22,33}.LastIndexOf(33)
2
Integer (Int32)

Remove(Object) - Removes the first occurrence of a specified object in a list.

Example Method Example Return Return Type
{11,22,33,33}.Remove(33)
{11,22,33}
List

RemoveAll(Object) - Removes all occurrences of a specified object in the list.

Example Method Example Return Return Type
{11,22,33,33}.RemoveAll(33)
{11,22}
List

RemoveAt(Integer) - Removes the element at the specified index from the list.

Example Method Example Return Return Type
{11,22,33}.RemoveAt(0)
{22,33}
List

Reverse - Reverses the order of the list.

Example Method Example Return Return Type
{11,22,33}.Reverse
{33,22,11}
List

ToArray - Copies the elements in the list to an array.

Example Method Example Return Return Type
{11,22,33}.ToArray
{11,22,33}
Array