How do you find common number in two lists in Python?
In this program, we will learn how to print all common numbers from two lists. We will see two methods. In method-1, we will find the common number from two lists and in method-2, we will find the common character in two strings.
Method – 1 Find the Common Number between two list
In this method, we will take two empty lists num1 and num2. Then we will create a function and we will pass two arguments inside the function. We will use for loop to retrieve the data from the list num-1 and then we will compare each data with the data of num-2 using the if statement. If the condition is satisfied then we append the data into another empty list.
Example:
num1=[]
num2=[]
#now we have to find from num1 and num2 common number
# is there or not if we found the common number then we will print that.
def common_num(num1,num2):
num = []
for i in num1:
if i in num2:
num.append(i)
return num
number1=[1,2,3,3,12,34,23,56,34,56,23,57,78,57,88,90]
number2=[1,3,6,12,34,56,23,56,23,67,50,45,89,90]
print(f'Common Numbers in num1{number1} and num2{number2} are: \n',
common_num(number1, number2))
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Common Numbers in num1[1, 2, 3, 3, 12, 34, 23, 56, 34, 56, 23, 57, 78, 57, 88, 90] and num2[1, 3, 6, 12, 34, 56, 23, 56, 23, 67, 50, 45, 89, 90] are:
[1, 3, 3, 12, 34, 23, 56, 34, 56, 23, 90]
Process finished with exit code 0
Method – 2 Find the Common Character between two Strings
In this metho-2, we will follow the same rule as method-1 but the difference is that there was a number of lists, and here will be String.
Example:
str1=[]
str2=[]
#now we have to find common characters from str1 and str2.
# If we found then we will print that characters.
def common_num(str1,str2):
string = []
for i in str1:
if i in str2:
string.append(i)
return string
string1 = "My Programming School"
string2 = "Madanapalle Institure of Technology and Science"
print(f'Common Numbers in num1{string1} and num2{string2} are: \n',
common_num(string1, string2))
Output:
C:\Users\Pramod\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/Pramod/PycharmProjects/pythonProject/main.py
Common Numbers in num1My Programming School and num2Madanapalle Institure of Technology and Science are:
['M', 'y', ' ', 'r', 'o', 'g', 'r', 'a', 'i', 'n', 'g', ' ', 'S', 'c', 'h', 'o', 'o', 'l']
Process finished with exit code 0
Recommended Post:
- Python Hello World Program
- Python Comment | Creating a Comment | multiline comment | example
- Python Dictionary Introduction | Why to use dictionary | with example
- How to do Sum or Addition in python
- Python Reverse number
- find the common number in python
- addition of number using for loop and providing user input data in python
- Python Count char in String
- Python Last Character from String
- Python Odd and Even | if the condition
- Python Greater or equal number
- Python PALINDROME NUMBER
- Python FIBONACCI SERIES
- Python Dictionary | Items method | Definition and usage
- Python Dictionary | Add data, POP, pop item, update method
- Python Dictionary | update() method
- Delete statement, Looping in the list In Python
- Odd and Even using Append in python
- Python | Simple Odd and Even number
Get Salesforce Answers