Find Common Number in Python

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:

Get Salesforce Answers

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment