In this tutorial, we will learn how to compare elements of two arrays in Python.
Comparing elements of two arrays is a common operation in any programming language and can be used in a variety of applications including comparing matrices, checking differences between lists, or finding items in common.
Here, we will use the built-in Python list and NumPy arrays for demonstration.
Steps
Follow the steps below to compare elements of two arrays in Python.
Step 1: Create two arrays
First, let’s create two Python lists named list1 and list2:
1 2 |
list1 = [1, 2, 3, 4, 5] list2 = [4, 5, 6, 7, 8] |
Additionally, we will create two NumPy arrays named array1 and array2. To do this, we will need to import the NumPy library first. If you don’t have NumPy installed, you can install it using the following pip command:
1 |
pip install numpy |
Now we import NumPy and create our arrays:
1 2 3 4 |
import numpy as np array1 = np.array([1, 2, 3, 4, 5]) array2 = np.array([4, 5, 6, 7, 8]) |
We have now created two Python lists and two NumPy arrays.
Step 2: Compare elements using a for loop
We can use a for loop to iterate over the elements of both lists and compare them.
1 2 3 4 |
for element1 in list1: for element2 in list2: if element1 == element2: print("Equal elements from lists:", element1, element2) |
Step 3: Compare elements using list comprehensions
A more concise way to achieve the same task is by using list comprehension. This will essentially create a new list with the common elements between the two lists:
1 2 |
common_elements = [element1 for element1 in list1 for element2 in list2 if element1 == element2] print("Common elements between lists:", common_elements) |
Step 4: Compare elements of NumPy arrays using NumPy functions
Now, let’s do the same with NumPy arrays. Instead of using loops or list comprehensions, we can take advantage of the np.intersect1d() function, which automatically returns common elements between two arrays:
1 2 |
common_elements_np = np.intersect1d(array1, array2) print("Common elements between NumPy arrays:", common_elements_np) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import numpy as np list1 = [1, 2, 3, 4, 5] list2 = [4, 5, 6, 7, 8] array1 = np.array(list1) array2 = np.array(list2) for element1 in list1: for element2 in list2: if element1 == element2: print("Equal elements from lists:", element1, element2) common_elements = [element1 for element1 in list1 for element2 in list2 if element1 == element2] print("Common elements between lists:", common_elements) common_elements_np = np.intersect1d(array1, array2) print("Common elements between NumPy arrays:", common_elements_np) |
Output
Equal elements from lists: 4 4 Equal elements from lists: 5 5 Common elements between lists: [4, 5] Common elements between NumPy arrays: [4 5]
Conclusion
In this tutorial, we learned how to compare elements of two arrays in Python using both Python lists and NumPy arrays. We demonstrated how to use for loops, list comprehensions, and NumPy functions to achieve this task. Comparing elements of arrays is a useful skill to have and is applicable to a wide range of scenarios in both data analysis and programming in general.