Generate a binary values dictionary by searching in two lists

I have two lists


Expected Output


if the values of list1 are present in list2 then the values of output dictionary would be 1, otherwise 0.

>Solution :

If the size of list2 is small, you can use a simple dictionary comprehension:

{key: int(key in list2) for key in list1}

However, if list2 is long, you should turn it into a set for faster lookups:

lookup_set = set(list2)
{key: int(key in lookup_set) for key in list1}

Both of these output:

{'ABC': 1, 'DBA': 1, 'CBA': 0, 'RBA': 0, 'DCA': 0, 'RMA': 1}

Leave a Reply