Comparing Python nested lists

intersection, list, nested, python

Solution

If the lists only contain string tuples, then the easiest way to do it is using sets intersection (&):

>>> set([('EFG', '[3,4,5]'), ('DEF', '[2,3,4]')]) & set([('DEF', '[2,3,4]'), ('FGH', '[4,5,6]')])
set([('DEF', '[2,3,4]')])

Problem

I have two nested lists, each nested list containing two strings e.g.: ``` list 1 [('EFG', '[3,4,5]'), ('DEF', '[2,3,4]')] and list 2 [('DEF', '[2,3,4]'), ('FGH', '[4,5,6]')] ``` I would like to compare the two lists and recover those nested lists which are identical with each other. In this case only `('DEF','[2,3,4]')` would be returned. The lists could get long. Is there an efficient way to do this?

Original source