# Python – Print all the common elements of two lists

## Python | Print all the common elements of two lists

Given two lists, print all the common elements of two lists.
Examples:

Input : list1 = [1, 2, 3, 4, 5]
list2 = [5, 6, 7, 8, 9]
Output : {5}
Explanation: The common elements of
both the lists are 3 and 4

Input : list1 = [1, 2, 3, 4, 5]
list2 = [6, 7, 8, 9]
Output : No common elements
Explanation: They do not have any
elements in common in between them

### Method 1:Using Set’s & property

Convert the lists to sets and then print set1&set2. set1&set2 returns the common elements set, where set1 is the list1 and set2 is the list2.
Below is the Python3 implementation of the above approach:

 # Python program to find the common elements # in two lists def common_member(a, b):     a_set = set(a)     b_set = set(b)     if (a_set & b_set):         print(a_set & b_set)     else:         print("No common elements")             a = [1, 2, 3, 4, 5] b = [5, 6, 7, 8, 9] common_member(a, b)   a = [1, 2, 3, 4, 5] b = [6, 7, 8, 9] common_member(a, b)

Output:

{5}
No common elements

### Method 2:Using Set’s intersection property

Convert the list to set by conversion. Use the intersection function to check if both sets have any elements in common. If they have many elements in common, then print the intersection of both sets.
Below is the Python3 implementation of the above approach:

 # Python program to find common elements in # both sets using intersection function in # sets # function def common_member(a, b):        a_set = set(a)     b_set = set(b)          # check length     if len(a_set.intersection(b_set)) > 0:         return(a_set.intersection(b_set))      else:         return("no common elements")        a = [1, 2, 3, 4, 5] b = [5, 6, 7, 8, 9] print(common_member(a, b))   a =[1, 2, 3, 4, 5] b =[6, 7, 8, 9] print(common_member(a, b))

Output:

{5}
No common elements

Last Updated on November 11, 2021 by admin

## Matplotlib.pyplot.xticks() in PythonMatplotlib.pyplot.xticks() in Python

Matplotlib.pyplot.xticks() in Python Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. Pyplot is a state-based interface to a Matplotlib module which provides a MATLAB-like interface. matplotlib.pyplot.xticks() Function   The annotate() function in pyplot module of matplotlib library is used

## Python – Cloning or Copying a listPython – Cloning or Copying a list

Python – Cloning or Copying a list In this article we will learn various ways of copying or cloning a list in Python. Using slicing technique   This is the easiest and the fastest way to clone a list. This

## Taking input in PythonTaking input in Python

Let’s learn How to take input from User in Python language. Developers often have a need to interact with users, either to get data or to provide some sort of result. Most programs today use a dialog box as a

## StringIO Module in PythonStringIO Module in Python

StringIO Module in Python The StringIO module is an in-memory file-like object. This object can be used as input or output to the most function that would expect a standard file object. When the StringIO object is created it is initialized by

## Reading an image in OpenCV using PythonReading an image in OpenCV using Python

Reading an image in OpenCV using Python In this article, we’ll try to open an image by using OpenCV (Open Source Computer Vision). To use the OpenCV library in python, we need to install these libraries as a prerequisite:

## DateTimeField – Django ModelsDateTimeField – Django Models

DateTimeField – Django Models   In Django, DateTimeField is a field used to store a date and time, represented in Python by a datetime.datetime instance. It is mainly used to store an object of datetime created in Python. This article

## os.walk() in Pythonos.walk() in Python

os.walk() in Python How to traverse file system in Python ? Suppose we have given below file structure in our system and we want to traverse all it’s branches completely from top to bottom ? How does os.walk() work in

## Rename columns in Pandas DataFrameRename columns in Pandas DataFrame

If you’re working with data in Python using Pandas library, you might come across a scenario where you need to rename columns of a DataFrame. This is a common task in data cleaning and preprocessing, as well as data analysis