Find common elements in two lists in python

Find common elements in two lists Python

💡 Outline
You can first convert first list into set using set() and call intersection() by passing second list as parameter to find common element in two lists in Python.

In this tutorial, we will see different methods to find common elements in two lists in python.

Using the intersection() Function

This is the easiest method to find common elements in two lists in Python. As the name suggests, the intersection() function is a built-in python function that is used to return a set that contains the elements which are common in two sets. The sets can be of any form i.e a list or a dictionary.

Example:

Output:

{10, 20, 30}

Note that, __name__ variable is used in the above code. It is a built-in variable that is used to see whether the current code is being run on its own or an external module is being imported from somewhere. Thus, the __name__ variable checks the name of the current module in the ongoing code. It is usually used with an if statement and where it is assigned to '__main__' when there is no module in the program. Otherwise, __name__ is assigned to the name of the module used in the program.

Using List Comprehension

List Comprehension is a very concise way which helps in creating a list based on values stored from existing list or lists. This is also one way of finding common elements from two lists.

Example:

Output:

[10, 20, 30]

NOTE: There is one drawback in using this method to find common elements i.e this method should not be used in the case of large lists.

Using & Operator

The set’s & operator is also one way of finding common elements in two lists. In this method, both the lists are converted into sets first and then the common elements are found using &.

Example:

Output:

{10, 20, 30}
There are no common elements

Explantion

  • First both the lists are converted into set using set() function.
  • Used & to find common elements in two sets in Python.
  • We have used def keyword in above code. def` is used to define a function with more than one statement in a function. This function can be called with the help of the function name at the end of the code. In the above code, we have called the function twice at the end where we made two cases of the sets to find the common elements, one with some common elements and one with no common elements.

That’s all about how to find common elements in two lists in Python.

Related Posts

  • 07 January

    How to compare lists in Python

    Table of ContentsPython compare listsHow to compare lists in PythonUsing the sort() function and == operatorUsing the map() and reduce() functionsUsing the set() function and == operatorUsing the for loopUsing the set() function and - operatorUsing the collections.Counter() class and == operatorUsing the zip(), sum(), and len() functionsUsing the cmp() functionConclusion In this post, we […]

  • Python List of dictionaries
    09 December

    List of Dictionaries in Python

    Table of ContentsHow to Create a List of Dictionaries in Python?How to Access Elements of a List of Dictionaries?Search in a List of Dictionaries in PythonUpdate Values in a List of Dictionaries in PythonHow to Sort List of Dictionaries in Python?Conclusion We use both lists and dictionaries in our python programs. Lists are used to […]

  • Python sort list of lists
    17 November

    Sort List of Lists in Python

    Table of ContentsSort List of Lists Using the sort() Method in PythonUsing the itemgetter() methodUsing Lambda FunctionsSort List of Lists Using the sorted() Function in PythonSort lists of lists on the basis of the length of the inner listsConclusion Several times we use 2-D lists or lists of lists in our programs to represent a […]

  • Remove nan from list in Python
    16 November

    Remove nan From List In Python?

    Table of ContentsWhat is nan in python?How to remove nan from list in python?Remove nan from list using the str() functionRemove nan from list using math.isnan() functionRemove nan from list using numPy.isnan() functionRemove nan from list using pandas.isnull() functionConclusion In python, nan is a special value used to represent missing values. In this article, we […]

  • List intersection in Python
    11 November

    List intersection in python

    Table of ContentsWhat is the list intersection in Python?Ways to find the list intersection in PythonUsing the for loop to achieve list intersection in PythonUsing the set() function to find list intersection in PythonUsing the intersection() function to find list intersection in PythonUsing the filter() and lambda functions to find list intersection in PythonUsing the […]

  • Initialize list with zeros in Python
    25 October

    Four Ways To Initialize List With Zeros In Python

    Table of ContentsWhy Initialize List with Zeros in Python?How Can We Initialize List With Zeros In Python?Initialize List With Zeros Using * Operator In PythonInitialize List With Zeros Using  itertools.repeat() FunctionInitialize List With Zeros Using  Generator Comprehension In PythonInitialize List With Zeros Using  List Comprehension In PythonConclusion Lists in Python are one of the most […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.