# FQL - contains in a list

**URL:** <https://community.postman.com/t/fql-contains-in-a-list/60336>\
**Category:** Help Hub\
**Tags:** postman-flows, evaluate-block\
**Created:** [April 25, 2024, 10:21am UTC](https://community.postman.com/t/fql-contains-in-a-list/60336 "2024-04-25T10:21:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bknathan](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@bknathan](https://community.postman.com/u/bknathan)\
**Post date:** [April 25, 2024, 10:21am UTC](https://community.postman.com/t/fql-contains-in-a-list/60336/1 "2024-04-25T10:21:00Z")

</div>

Hi,

In Postman flows i need to compare two lists in FQL itself in evaluate block.  
Work around is to add multiple for-evaluate-collect blocks for this. But that makes the flow cumbersome when i need to compare different lists in same flow.

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/c/2/c2ec9ca1fbb3aca869e8b3c200fb66f7ffc3525c.png)

---

<div class="post-metadata">

**Author:** ![flows-daniel](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/flows-daniel/32/25516_2.png) [@flows-daniel](https://community.postman.com/u/flows-daniel)\
**Post date:** [April 25, 2024, 6:27pm UTC](https://community.postman.com/t/fql-contains-in-a-list/60336/2 "2024-04-25T18:27:08Z")

</div>

Hi @bknathan

What are you trying to compare between the two lists? Just checking to see if they are exactly the same or something more? What do you need the output to be?

---

<div class="post-metadata">

**Author:** ![bknathan](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@bknathan](https://community.postman.com/u/bknathan)\
**Post date:** [April 25, 2024, 6:53pm UTC](https://community.postman.com/t/fql-contains-in-a-list/60336/3 "2024-04-25T18:53:45Z")

</div>

Hi @flows-daniel ,

I am trying to compare 2 lists and find their items of all-unique, intersection, only list-1, only in list-2.  
For example,  
**Input to evaluate**  
List-1 = [a, b, c, d]  
List-2 = [c, d, e, f]

**Output from evaluate**  
{  
“all-unique” : [a, b, c, d, e, f],  
“common”: [c, d],  
“only in List-1”: [a, b],  
“only in List-2”: [e, f]  
}

---

<div class="post-metadata">

**Author:** ![flows-daniel](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/flows-daniel/32/25516_2.png) [@flows-daniel](https://community.postman.com/u/flows-daniel)\
**Post date:** [April 26, 2024, 12:11am UTC](https://community.postman.com/t/fql-contains-in-a-list/60336/4 "2024-04-26T00:11:42Z")

</div>

@bknathan

This will be significantly easier when TypeScript support is released after Post/Con (next week) since we’re really performing 4 different operations here. I will update my answer here after it’s available.

For now you can use this to get the desired result:

```auto
{
    "all-unique" : $distinct($append(list1, list2)),
    "common": $filter($map($distinct($append(list1, list2)), fn ($v) {
    $filter($append(list1, list2), fn ($v2) {
        $v2 = $v
    })[1]
}), fn ($v) {
    $v
}),
"only in List-1": $filter($map(list1, fn ($v) {
    $v in list2 ? null : $v
}), fn ($v) {
    $v
}),
"only in List-2": $filter($map(list2, fn ($v) {
    $v in list1 ? null : $v
}), fn ($v) {
    $v
})
}

```

 ![Screenshot 2024-04-25 at 5.02.37 PM](https://us1.discourse-cdn.com/getpostman/original/3X/0/2/029e65ae7d2adb599e655a122fd173e40b6fa0a7.png)

for all the uniques, just combine the lists (using [$append](https://learning.postman.com/docs/postman-flows/flows-query-language/function-reference/#append)) then use [$distinct](https://learning.postman.com/docs/postman-flows/flows-query-language/function-reference/#distinct) to get the unique ones.

For common ones, we take the unique list and [$map](https://learning.postman.com/docs/postman-flows/flows-query-language/function-reference/#map) through all the elements of the combined list of elements and [$filter](https://learning.postman.com/docs/postman-flows/flows-query-language/function-reference/#filter) for only where we found more than one match.

For the only in list 1 and only in list 2, we map over one list to check if the other list contains it, if so we return null, else the element, then filter for all the not nulls.

---

<div class="post-metadata">

**Author:** ![bknathan](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@bknathan](https://community.postman.com/u/bknathan)\
**Post date:** [April 26, 2024, 5:37pm UTC](https://community.postman.com/t/fql-contains-in-a-list/60336/5 "2024-04-26T17:37:23Z")

</div>

Working like a charm. Thank you @flows-daniel

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/system/32/29796_2.png) [@system](https://community.postman.com/u/system)\
**Post date:** [April 29, 2024, 5:38pm UTC](https://community.postman.com/t/fql-contains-in-a-list/60336/6 "2024-04-29T17:38:13Z")

</div>

Closed on April 29, 2024, 5:38pm UTC

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

---

<div class="post-metadata">

**Author:** ![flows-daniel](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/flows-daniel/32/25516_2.png) [@flows-daniel](https://community.postman.com/u/flows-daniel)\
**Post date:** [May 10, 2024, 9:29pm UTC](https://community.postman.com/t/fql-contains-in-a-list/60336/7 "2024-05-10T21:29:58Z")

</div>

Updating this with a TypeScript solution as well:

```auto
function compareLists(list1: string[], list2: string[]): any {
    const allUniqueSet = Array.from(new Set<string>([...list1, ...list2]));
    const common: string[] = [];
    const onlyInList1: string[] = [];
    const onlyInList2: string[] = [];

    list1.forEach(item => {
        if (list2.includes(item)) {
            common.push(item);
        } else {
            onlyInList1.push(item);
        }
    });

    list2.forEach(item => {
        if (!list1.includes(item)) {
            onlyInList2.push(item);
        }
    });

    return {
        "all-unique": allUniqueSet,
        "common": common,
        "only in List-1": onlyInList1,
        "only in List-2": onlyInList2
    };
}
compareLists(list1, list2);

```

will yield the same result.
