Solved

How to create variable for index and variable for item when looping a list

  • 23 February 2023
  • 7 replies
  • 616 views

Badge +4

How can I use the Loop action in order to create a variable for index, and create a variable for item?

For example, in Python, it would be like iterrows: https://www.w3schools.com/python/pandas/ref_df_iterrows.asp

In RPA, doing the following would only allow for creating a variable for the item, there is no option here for also creating a variable for index.

 

Thanks

icon

Best answer by bradwyatt 23 February 2023, 23:53

View original

7 replies

Userlevel 4
Badge +10

Hi @bradwyatt ,

while using loop iterator for List you get each value of a list in a string variable i.e. Item.

For Index, what actually you want to do can you please elaborate. As if you want the data of previous iteration you can store it in a separate variable and do the task 

Userlevel 7
Badge +13

Hi @bradwyatt ,

 

Looping a list is basically working based on the Index number only. The index is starting from 0 and the result of each iteration will be the list item of that particular index number with respect to current iteration.

 

 

Badge +4

Hi @bradwyatt ,

while using loop iterator for List you get each value of a list in a string variable i.e. Item.

For Index, what actually you want to do can you please elaborate. As if you want the data of previous iteration you can store it in a separate variable and do the task 

Let’s say I have a list (named $DepartmentList$) of 211 items (and each item is a string).

I want to loop through each of the items (with a $Counter$ variable). For each item (I’d like for there to be a way to reference this), I’d like to compare it to another string variable. If the comparison result is true, then I’ll be selecting its respective checkbox on my browser (this is through “Recorder: Capture”) by modifying the Path to include the item’s position in the list ($Counter$).

Badge +4

Here’s my solution, which works:

Before the loop, I assigned a $Counter$ variable to 1 (start of my iteration).

I used the loop for “List: For each item in list” with $ListItem$ as current value.

For the iteration, I used logic to compare $ListItem$ to another string variable.

At the end of the iteration, I incremented $Counter$ by 1.

Userlevel 4
Badge +10

Hi @bradwyatt ,

while using loop iterator for List you get each value of a list in a string variable i.e. Item.

For Index, what actually you want to do can you please elaborate. As if you want the data of previous iteration you can store it in a separate variable and do the task 

Let’s say I have a list (named $DepartmentList$) of 211 items (and each item is a string).

I want to loop through each of the items (with a $Counter$ variable). For each item (I’d like for there to be a way to reference this), I’d like to compare it to another string variable. If the comparison result is true, then I’ll be selecting its respective checkbox on my browser (this is through “Recorder: Capture”) by modifying the Path to include the item’s position in the list ($Counter$).

My solution would be : 

If you want to compare it with a another string then you can directly use the output value of the list loop and compare it with your respective string. No need to use a counter variable to reference. 

Userlevel 7
Badge +13

Here’s my solution, which works:

Before the loop, I assigned a $Counter$ variable to 1 (start of my iteration).

I used the loop for “List: For each item in list” with $ListItem$ as current value.

For the iteration, I used logic to compare $ListItem$ to another string variable.

At the end of the iteration, I incremented $Counter$ by 1.

 

 

But why you require the Index number if you already have the current item in the list within the loop itself?

 

It is not an Excel, Data Table or Database iteration.

Badge +4

Here’s my solution, which works:

Before the loop, I assigned a $Counter$ variable to 1 (start of my iteration).

I used the loop for “List: For each item in list” with $ListItem$ as current value.

For the iteration, I used logic to compare $ListItem$ to another string variable.

At the end of the iteration, I incremented $Counter$ by 1.

 

 

But why you require the Index number if you already have the current item in the list within the loop itself?

 

It is not an Excel, Data Table or Database iteration.



My original post must’ve not included it, but the reason I needed the index number too is because I was also referencing the index number in xpath when using a Recorder: Capture action to click a checkbox in the respective location.

Reply