
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python program to print all the common elements of two lists.
Given two lists, print all the common element of two lists.
Examples −
Input : L1 = [5, 6, 7, 8, 9] L2 = [5, 13, 34, 22, 90] Output : {5}
Explanation
The common elements of both the list is 5.
Algorithm
Step1 : create two user input lists. Step2 : Convert the lists to sets and then print set1&set2. Step3 : set1 and set2 returns the common elements set, where set1 is the list1 and set2 is the list2.
Example Code
# Python program to find the common elements # in two lists def commonelemnets(a, b): seta = set(a) setb = set(b) if (seta & setb): print("Common Element ::>",seta & setb) else: print("No common elements") A=list() n=int(input("Enter the size of the First List ::")) print("Enter the Element of First List ::") for i in range(int(n)): k=int(input("")) A.append(k) B=list() n=int(input("Enter the size of the Second List ::")) print("Enter the Element of Second List ::") for i in range(int(n)): k=int(input("")) B.append(k) commonelemnets(A,B)
Output
Enter the size of the First List :: 4 Enter the Element of First List :: 4 5 6 7 Enter the size of the Second List :: 4 Enter the Element of Second List :: 1 2 3 4 Common Element ::> {4} Enter the size of the First List :: 4 Enter the Element of First List :: 1 2 3 4 Enter the size of the Second List :: 4 Enter the Element of Second List :: 5 6 7 8 No common elements
- Related Articles
- C# program to print all the common elements of two lists
- Python program to find common elements in three lists using sets
- Python Program that print elements common at specified index of list elements
- Find common elements in list of lists in Python
- Adding two Python lists elements
- Program to interleave list elements from two linked lists in Python
- Minimum Index Sum for Common Elements of Two Lists in C++
- Python program to check if two lists have at least one common element
- Program to find minimum difference between two elements from two lists in Python
- Python program to split the even and odd elements into two different lists.
- Python program to print all distinct elements of a given integer array.
- Python program to find Intersection of two lists?
- C# program to find common values from two or more Lists
- Python program to concatenate every elements across lists
- Python program to find Cartesian product of two lists

Advertisements