Python Lab · Experiment 11
Find common values between two arrays
PythonWeek 3JNTUH R22 · CS206ES
Aim
To write a Python program that reads two arrays of integers and finds the values common to both, first with NumPy's intersect1d function and then with plain Python loops.
Algorithm
- Read the elements of the first array as one line of numbers separated by spaces, and convert them to a NumPy array.
- Read the second array in the same way.
- Find the common values with
np.intersect1d(a, b)and print them. - To do the same without NumPy, start with an empty list
common. - For each value x in the first array, if x is in the second array and not already in
common, add it tocommon. - Print
common.
Program
import numpy as np
first = input("Enter elements of the first array: ").split()
second = input("Enter elements of the second array: ").split()
a = np.array([int(x) for x in first])
b = np.array([int(x) for x in second])
print("First array :", a)
print("Second array:", b)
# Method 1: NumPy function
print("Common values (np.intersect1d):", np.intersect1d(a, b))
# Method 2: plain Python loops
common = []
for x in first:
value = int(x)
if value in b and value not in common:
common.append(value)
print("Common values (loop) :", common)Output
Enter elements of the first array: 10 25 3 7 25 18 42
Enter elements of the second array: 7 42 9 25 11
First array : [10 25 3 7 25 18 42]
Second array: [ 7 42 9 25 11]
Common values (np.intersect1d): [ 7 25 42]
Common values (loop) : [25, 7, 42]Recorded by running this exact program. Typed input appears after each prompt.
How it works
np.intersect1d returns the common values sorted in ascending order, with each value once, even if it repeats in an input. The loop keeps the values in the order they appear in the first array: here 25 comes before 7 because it appears first.
value in b works on a NumPy array as well as on a list. Another plain-Python way is with sets: set(first) & set(second) gives the common values, though a set has no fixed order.
The program expects whole numbers separated by spaces. Typing a letter stops it with a ValueError.
Common mistakes
Comparing the strings from
split()with numbers."7"and7are different values in Python, so convert every item withint()first.Adding a value to the result every time it matches, so a value that repeats in the first array appears twice. Check
value not in commonbefore appending.Using
a == bto find common values. That compares the arrays position by position and fails when the lengths differ.Expecting
intersect1dto keep the original order. It always returns the values sorted.
Viva questions
Try to answer each one aloud before you open it.
What does np.intersect1d do?
It returns a sorted array of the unique values that are present in both input arrays.
How can you find common elements of two lists without NumPy?
Loop over one list and keep each item that is also in the other list and not already kept, or convert both to sets and use the
&(intersection) operator.What does split() return when called with no argument?
A list of the words in the string, split at any run of spaces, tabs or newlines, with leading and trailing spaces ignored.
What is the time cost of the loop method?
For each item of the first array it may scan the whole second array, so it takes about n × m steps. Sets or
intersect1dare faster for large arrays.