328
Views
16
Comments
Solved
Removing an element from a list while iterating
Question
Application Type
Reactive

In the attached screenshot you can see that when the script executes it iterates through a list of selected values to determine if the value already exists in the list.  If not, it appends it.  If it does exist however, I want to remove that item from the list.  I am aware that the ListRemove cannot exist where it does in the screenshot because I would be modifying a list as I iterate through it.  

But if the iteration finds that a value exists, how can I grab the location of the list item to a variable if the last step in the process is an "End".  If the value exists, processing ends at the End so I don't have the opportunity to remove my list item.

Any help is appreciated.

--Rick


gr

2025-12-23 12-24-14
Adão Pedro
Solution


In your condition, your indexof takes the attribute of the list you are comparing, not the current value of the list

2019-04-09 00-57-55
carl ruhle

Richard, 

create an empty list an append to that new list the values that you want,  this way on the cycle you just append what you need. At the end you will have a list just with the values that you want. 

Hope this can help you 

Regards 

UserImage.jpg
Richard Dwyer

Thank you, but I'm not sure I follow.  Where in the flow I posted would the creation of a new list occur?


--Rick

2025-12-23 12-24-14
Adão Pedro

From what I understand, it doesn't need a cycle, just use the filter list, if it returns empty, it adds the value to the list, if it returns a value, it takes the index and removes it.

Captura de ecrã 2023-09-28 221510.jpg
UserImage.jpg
Richard Dwyer

Adao, I have configured my script to match yours (Screenshot attached at bottom).  This behaves the way I want it to if there is only 1 element in the list.  For example, if I click the 10 AM time slot, I correctly append 10 AM to the list.  If I click 10 AM again, the script removes the 10 AM entry because it already exists.  Great.

But if I click 10 AM, 2 PM and 4 PM, when I click 2 PM to remove it, it fails to recognize it's already in the list and it adds it again:



So I THINK the issue is that I'm not using ListFilter correctly.  In the following screenshot you can see that for ListFilter, I'm examining the list that I've added the times to and I'm specifying the condition that time being passed in matches the time from the list:


My expectation with the above is that when I pressed on the 2 PM button again, the list filter would return to me a new list with just that value in it.  Then my IF can test for a time <> Null and if true, ListRemove, otherwise add it.  But this is not working. 

I don't think I'm understanding how ListFilter is being used here.


--Rick



Screenshot 2023-09-29 at 9.43.50 AM.png
2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Adão,

This is bad practice, since you're not interested in the result of the ListFilter. In that case, a ListAny would be better.

However, since you are then doing a ListIndexOf, why not just do that directly?

2025-12-23 12-24-14
Adão Pedro


Yes, you don't need the list filter, you can work directly with the result from the ListIndexOf, the other steps are unnecessary. My fault. Thank you Killian

2019-04-09 00-57-55
carl ruhle

Rick,

you will have to change the flow.

On the cycle, if the value if not found append it to the new list.

If it is found do not append to the new list.

The listAppend that is on the flow after the cycle, that step I do not understand, sorry for that.

- create newList

-cycle on the valuesList 

-- if value does not exist in valuesList

-----append current value of valuesList into newList

--end-if

-end-cycle

From what I can understand from the screen shot, there can only be on "repeated" value? On value found as equal the process end's.

Hope that I was possible to be more clear, if not, we all will try our best to help you.


2021-06-01 05-56-33
Komal Kumbhar

Hello Richard,

First thing you can not end the loop on END NODE as you are using FOR EACH, 

For each loop needs to end on cycle it self.

To remove the existing record from the list use ListIndex to find out its positing and then Use ListRomove

But you can not use list remove on the iterating list as its not allowed to remove one of the record from the list while list is been iterating. You need to create copy of iterating list and remove record from it.

I have attached the OML here, you will find it under the drop down screen,


Hope this helps,

Komal

drodown.oml
2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Komal,

You are wrong! It is perfectly possible to end a loop with an End Node. Which you could've easily found out yourself if you had tested it. Don't spread misinformation!

2021-06-01 05-56-33
Komal Kumbhar

I am sorry, I 'll definitely try it out.

2020-09-15 13-07-23
Kilian Hekhuis
 
MVP

Hi Richard,

You can ignore most "advise" from the people above, as they have wrong, or bad practice solutions.

If you want to remove an element from a List, just use ListIndexOf in combination with a ListRemove. Only execute the ListRemove if ListIndexOf returns a value >= 0 (it returns -1 if it can't find a match).


UserImage.jpg
Richard Dwyer

All,

I appreciate the help and the different perspectives.  However, whether I use ListFilter or ListAny or ListIndexOf, they all fail.  Here I have clicked 3 separate buttons for 3 different times and you can see they all got assigned to my list:


My button to pass the selected time to the script contains an input parameter called "TimeSelected". In my ListAny or ListFilter or List IndexOf my configuration looks like this:



So you can see I'm doing a conditional test to see if my input parameter TimeSelected equals the "StartTime" value in my list.  But this fails to return as true when in fact it does match.

Is my issue that I'm using:

NewAppointmentData.DateTime.Current.StartTime = TimeSelected

Should I instead be using the following:

NewAppointmentData.DateTime.StartTime = TimeSelected

The second one seems to make more sense but then it doesn't validate.

--Rick

2025-12-23 12-24-14
Adão Pedro

Is your logic with listIndexOf like this?

UserImage.jpg
Richard Dwyer

Yes it is.  I just tested it again as well.  My script works to remove the time from the list if it's the only value in the list.  As soon as I have more than 1 value it fails to remove it and instead adds it so I have duplicates.

Heres the output of my list to the screen after 3 button clicks:

Here is how my ListIndex is configured:



And here I output the result of that ListIndex:




Here's the result if I click 10 AM again:



--Rick

2025-12-23 12-24-14
Adão Pedro
Solution


In your condition, your indexof takes the attribute of the list you are comparing, not the current value of the list

UserImage.jpg
Richard Dwyer

Thanks so much to all.  Using ListIndex combined with Adao's code revisions worked!


--Rick

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.