Iterate over all entries and get the list value (get value-name=list) and add to an array, similar to your foreach at the end
remove duplicates from array which would need to be another script
As long as you don’t have a lot of different lists, should still be doable, by de-duplicating the array of list names. Best in a separate script.
But since there is no way to check if an element is part of an array, except comparing it with each entry, if there are a lot of lists, it’s going to be slow ~O(n²)!
Algorithm could be:
duplicates array: dups
result array: uniqs
for each element in dups, do
iterate over all elements of uniqs
if match found -> continue (with next entry of dups)
add to uniqs