Perform mathematical calculation on a two Lists

Hi Folks,

I have two separate lists of type list, listActual and listBudget. I need to subtract listBudget from listActual (listActual - listBudget) and store this in a third list. Please help me out with step by step detailed method on how to achieve this. :slight_smile:

Hi @shikharno.7

Try this:
listResult = listActual.Zip(listBudget, Function(a, b) a - b).ToList()

1 Like

Hey @shikharno.7 ,
You can create a for each loop and drop an assign activity
and do as below equation

For Each item in listActual:
Assign listResult(index) = listActual(index) - listBudget(index)

Hope it helps you out

1 Like

@shikharno.7

  1. Create three variables of type List(Of T) for listActual, listBudget, and listResult, where β€˜T’ is the data type of your lists.
  2. Initialize listActual and listBudget with their respective data.
  3. Use the Assign activity to perform the subtraction using LINQ and store the result in listResult. Here’s how you can do it:

listActual.Zip(listBudget, Function(a, b) a - b).ToList()

This LINQ query uses the Zip method to combine the elements of both lists and then applies a lambda function to subtract corresponding elements, producing the listResult.
After the Assign activity, you can use the listResult variable in UiPath as needed.Preformatted text

1 Like

Hi @shikharno.7

=> Initialize the values in the listBudget and listActual.
=> Insert assign activity and create a variable Count which in integer datatype and initialize it as 0.
- Assign β†’ Count = 0
=> Iterate the one list called listActual by using the for each activity.
=> Insert the assign activity inside the for each and create a variable output as double.
- Assign β†’ output = listActual(Count)-listBudget(Count)
=> After this use append collection activity to append the value to the thirdlist which means finallist.
=> After append collection activity insert the assign activity to increment the Count.
- Assign β†’ Count = Count+1

Hope it helps!!

1 Like

Hi @shikharno.7

Please find below xaml for your reference

Sequence3.xaml (7.2 KB)

I hope it helps!!

1 Like

This method worked, thanks @supriya117

1 Like

Use an assign statement with left side as resultantList and right side as
Enumerable.Range(0, list1.Count).ToDictionary(function(i) i,Function(i) Cint(list1(i))-Cint(list2(i))).Values.ToList

image

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.