# Given a list made up of subsets of an alphabet, how can I use associations to create a Boolean dictionary of the given list

I have a list of 30,000 elements, each of the element is a list in itself {{a, b}, {p, q, r}, {a, p}, ..., {z, x}}. Second one contains unique elements {a, b, c, ..., z}.

I want to create an association which associates all the unique elements to the first list so that it shows whether the corresponding element is present in the first list. So, it should look something like this:

{
{<|a -> True, b -> True, c -> False, ..., z -> False|>},
{<| a -> False, b -> False, ..., p -> False, q -> False, r -> False, ..., z -> False |>},
...
}


Is there a way I can do this without loops?

• Welcome to Mathematica.SE! I suggest that: 1) You take the introductory Tour now! 2) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! 3) As you receive help, try to give it too, by answering questions in your area of expertise. Commented Jan 30, 2015 at 19:52
• What I can't understand is why you want to wrap each association in List. Why not just a list of associations? Commented Jan 30, 2015 at 22:12
• @m_goldberg I didn't quite get you, you mean '{<|a -> True, False ... |>, <| b -> True, True ... |>, ... }' ? Commented Jan 31, 2015 at 4:47
• I mean {{<|a -> True, b -> True, c -> False, ..., z -> False|>}, {<| a -> False, b -> False, ..., p -> False, q -> False, r -> False, ..., z -> False |>}, ...}. Commented Jan 31, 2015 at 12:15
• yes, this is exactly I want and the answer of @kuba gives this output :) Commented Feb 1, 2015 at 4:35

 ClearAll @@ CharacterRange["a", "z"]

elements = Symbol /@ CharacterRange["a", "z"]

main = Association @ Thread[elements -> False]

<|a->False,b->False,c->False ... x->False,y->False,z->False|>

set = {{a, b}, {p, q, r}, {a, p}}

Merge[{Association@Thread[# -> True], main}, Or @@ # & ] & /@ set

{<|a -> True, b -> True, c -> False, ... x -> False, y -> False, z -> False|>,
<|p -> True, q -> True, r -> True, a -> False, ... z -> False|>,
<|a -> True, p -> True, b -> False, ... y -> False, z -> False|>}

• I'm not very familiar with Associations so it may not be the best approach.
– Kuba
Commented Jan 30, 2015 at 19:57
• Just for my understanding, you first created a association with all False values, then you merged this association with another association containing True values for the corresponding elements from the list. Am I correct? Commented Jan 30, 2015 at 21:17
• @babbupandey Yes, first main and then every sublist is (while mapping) converted in similar way to ->True and marged with main, duplicates are handled with Or function which will convert a -> {True, False} to a -> Or@@{True,False} == True
– Kuba
Commented Jan 30, 2015 at 21:21
• Ah, ok! I accepted it because this worked pretty fast compared to the loops I was running :-) Commented Jan 30, 2015 at 21:36
• @babbupandey I'm glad it did. Loops are usually not so fast in Mathematica. You may be interested in Alternatives to procedural loops.
– Kuba
Commented Jan 30, 2015 at 21:38