In this tutorial, you will learn how to write Python program to compare two arrays and check that they are equal in size or not.
In Python, arrays (often implemented as lists) are used frequently to store sequences of items. Sometimes, we need to check if two arrays are the same size. This means making sure they have the same number of elements. This is useful for tasks like comparing data or ensuring that operations like element-wise addition can proceed without errors. There are a few different ways to accomplish this in Python, each suited to different situations or coding styles. Let’s explore how you can check if two arrays are equal in size using Python.
Example
Consider the following two arrays:
arr1 = [1, 2, 3, 4, 5]
arr2 = [1, 3, 4, 5, 7]
Here the size of both array arr1 and arr2 are same which is 5, so the output should be like “arrays are same in size”
Python Program to Check if Two Arrays are Equal in Size
Program 1: Conditional Expression Using len() Function
For a more explicit check, especially useful in if-else structures for handling different sizes differently, you might use a conditional expression directly.
arr1=[1,2,3,4,5]
arr2=[1,3,4,5,7]
if len(arr1) == len(arr2):
print("arrays are equal in size")
else:
print("arrays are not equal in size")
Output:
arrays are equal in size
Explanations
This simple Python program checks if two lists, arr1
and arr2
, have the same number of elements. Both lists contain a series of numbers, but they don’t necessarily have the same numbers. The program uses the len()
function, which finds out how many items are in each list. If both lists have the same number of items, it prints “arrays are equal in size” to tell the user that the lists are the same length. If the lists are different in length, it prints “arrays are not equal in size.” This kind of check is useful to make sure two lists can be compared or processed together without errors due to differing lengths.
Program 2: Conditional Expression Using len() Function
The most straightforward way to check if two arrays are the same size is to use Python’s built-in len()
function, which returns the number of items in a list.
arr1=[1,2,3,4,5]
arr2=[1,3,4,5,7]
are_equal = len(arr1) == len(arr2)
print("Are arrays equal in size?", are_equal)
Output:
Are arrays equal in size? True
Explanations
This Python program checks whether two lists, arr1
and arr2
, contain the same number of elements. It uses the len()
function to find the size of each list. The program then compares these sizes to see if they are equal by using the equality operator (==
). The result of this comparison is stored in the variable are_equal
, which will be True
if the lists are the same size, and False
if they are not. Finally, the program prints a message indicating whether the arrays are equal in size, displaying either True
or False
. This check is useful in scenarios where you need to ensure that two lists are compatible in size for further operations or processing.
Conclusion
Checking if two arrays are equal in size is a simple yet important task in Python programming. Whether you’re preparing data for analysis, ensuring conditions for algorithm requirements, or just comparing lists, knowing how to perform this check effectively can save time and prevent errors. The methods given in this article provide a good starting point for implementing to check if two arrays are equal in size or not.