How to find differences between 2 lists?

Say I have 2 lists with values inside:
list1 = [4, 3, 1, 2, 5]
list2 = [1, 2, 3, 4, 5, 6, 7]

I’d like to return a list containing the difference from these 2 lists, to get [6, 7]
Order within the list isn’t always sorted.

Side question that could be another topic: How do I ensure that all the elements within the list are unique? How does one remove items from a list through the action flow without manually editing the list?

Seems like I have found the solution:
I found it challenging to complete this issue using the provided actions so a simple custom script solves this.

       input = ['list1', 'list2'],
       output = 'diff'

    def listDiff() {
        def java_list1 = list1.asList()
        def java_list2 = list2.asList()
        diff = (java_list2 - java_list1) as RList

This takes the difference of list2 against list1. If you want to find the symmetric difference between the 2 lists i.e. you want the values missing in both list1 and list2, the logic just needs to be (list2 - list1) + (list1 - list2)


Thank for sharing @tinwy nice script :slightly_smiling_face:

1 Like

@ashapkina I think it would be great to have a subforum dedicated to custom user scripts like this, where anyone could post their solutions.

1 Like

Thank you, @Piotr good idea! :+1: I will think how to organize it!

1 Like