Change language

# Python | Find common items in a list of lists

|

Method # 1: Using ` reduce () + lambda + set () `
This particular task can be achieved in one line using a combination of the above functions. The decrease function can be used to control the "& amp;" for the whole list. The set function can be used to convert a list to a set to remove duplicates.

 ` # Python3 demo code ` ` # general form of extracting elements from N lists ` ` # using Reduce () + Lambda + Set () ` ` from ` ` functools ` ` import ` ` reduce `   ` # initializing the list of lists ` ` test_list ` ` = ` ` [[[` ` 2 ` `, ` ` 3 ` `, ` ` 5 ` `, ` ` 8 ` `], [` ` 2 ` `, ` ` 6 ` `, ` ` 7 ` `, ` ` 3 ` `], [` ` 10 ` `, ` ` 9 ` `, ` ` 2 ` `, ` ` 3 ` `]] ` ` `  ` # print original list ` ` print ` ` (` `" The original list is: "` ` + ` ` str ` ` (test_list) ) `   ` # general form for extracting elements from N lists ` ` # using Reduce ( ) + Lambda + Set () ` ` res ` ` = ` ` list ` ` (` ` reduce ` ` (` ` lambda ` ` i, j: i & amp; j, (` ` set ` ` (x) ` ` for ` ` x ` ` in ` ` test_list))) `   ` # print result ` ` print ` ` (` ` "The common elements from N lists:" ` ` + ` ` str ` ` (res)) `

Exit:

` The original list is: [[2, 3, 5, 8], [2, 6, 7, 3], [10, 9, 2 , 3]] The common elements from N lists: [2, 3] `

Method # 2: Using ` map () + intersection () `
The ` map ` function can be used to transform each of the lists into sets that need to be manipulated to perform the intersection using the set.intersection function. This is the most elegant way to accomplish this particular task.

 ` # Python3 demo code ` ` # general form for extracting elements from N lists ` ` # using map () + intersection () ` ` `  ` # initializing the list of lists ` ` test_list ` ` = ` ` [[` ` 2 ` `, ` ` 3 ` `, ` ` 5 ` `, ` ` 8 ` `], [` ` 2 ` `, ` ` 6 ` `, ` ` 7 ` `, < / code> 3 ], [ 10 , 9 , 2 , 3 ]] ``   # print original list print ( "The original list is:" + str (test_list))   # general form of extracting elements from N lists # using map () + intersection () res = list ( se t . intersection ( * map ( set , test_list)))   # print result print ( "The common elements from N lists:" + str (res)) `

Exit:

` The original list is: [[2, 3, 5, 8], [2, 6, 7, 3], [10, 9, 2, 3]] The common elements from N lists: [2, 3] `

## Shop

Learn programming in R: courses

\$

Best Python online courses for 2022

\$

Best laptop for Fortnite

\$

Best laptop for Excel

\$

Best laptop for Solidworks

\$

Best laptop for Roblox

\$

Best computer for crypto mining

\$

Best laptop for Sims 4

\$

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

NUMPYNUMPY

How to convert Nonetype to int or string?

NUMPYNUMPY

How to specify multiple return types using type-hints

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

## Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method