Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>>> 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))))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading