I'm familiar with finding the intersection of two lists, however, I wanted to find the union of two lists in tcl (while eliminating duplicates). I do have a working copy of this code, but I'm not sure it is robust enough for any kind/number of lists and am hence looking for a better solution.
Any help or ideas are appreciated.
If you treat lists as sets, so you don't worry about order if the items, you could just sort the joined list:
set union [lsort -unique [list {*}$list1 {*}$list2]]
Tclx provides a union command:
% info patchlevel
8.5.9
% set a [list a b c]
a b c
% set b [list a d e]
a d e
% package require Tclx
8.4
% union $a $b
a b c d e
%
% union
wrong # args: should be "union lista listb"
%
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With