Find common values in i number of lists

I wish to do what is described in this previous question but instead of lists a,b,c I have i number of lists (that is set based on user input):

Find common values in multiple lists

How can I change the code from this question (shown below) to work with i number of lists?

>>> a = [1, 2, 3, 4]
>>> b = [2, 3, 4, 5, 6]
>>> c = [3, 4, 5, 6, 10, 12]
>>> elements_in_all = list(set.intersection(*map(set, [a, b, c])))
>>> elements_in_all
[3, 4]

The issue I have is i don’t know how many lists I have in advance! needs to be iterable somehow

>Solution :

if you get input from the user, try to store it in the list of lists,like

lists = [[1,2,3], [1,2]]

then, use that variable instead of [a, b, c], map internally iterates all these lists and convert them to set.

li = [[1,2,3], [1,2]]
print(list(set.intersection(*map(set, li))))

Leave a Reply