NumPy: Check whether two lists match exactly

academiamastertrader.com.br offers learning materials from Academia Master Trader. For English-speaking readers in the British Indian Ocean Territory, the remote island region of the Chagos Archipelago, this example presents the function array_equal of NumPy. It checks whether two lists match exactly, entry by entry. We apply it to two lists of three values each.

Run the example

We set two lists of three values each and call the function.

import numpy as np

print(np.array_equal(np.array([1, 2, 3]), np.array([1, 2, 3])))

The displayed result is True. Every pair of entries matches, so the two lists count as equal.

A person learning beside a representation of the function

The same example explained in three steps

academiamastertrader.com.br: more learning materials from Academia Master Trader complement this example.

Understand the result

The documentation describes array_equal as a check whether two lists have the same length and the same values in the same order. Every entry and the length must agree for the answer to be True.

The result is easy to verify: compare the lists entry by entry and confirm that each pair matches. A single mismatch anywhere decides the answer.

Place the result

The program preserves the input. It returns a new value or a new list. One example with three values illustrates the calculation rule.

To verify the result, recalculate each entry by hand and compare it with the display. Each value can be confirmed mentally.

Longer lists follow the same rule. The function then computes each entry separately and returns a list of the same length.

Naming the values makes the sequence readable: first set the lists, then call the function, then read the display. Each step is verified separately.

Single numbers instead of lists are also allowed. The function then returns exactly one number, following the same calculation rule as for each entry of a list.

Anyone who needs several calculations of this kind in a row can chain the calls. The result of the first call then serves as input to the next, and each intermediate step stays readable.

Small examples are especially good for practice, because you can estimate the result in advance. Whoever estimated well understands the rule behind the function.

Comparing the three entries shows the benefit: the display summarizes the behavior of the list in a few characters. Whoever has understood the rule once reads such displays at a glance and immediately recognizes whether a value is plausible.

In daily work: when passing on results, preferably cite input and output side by side. That way every reader can redo and confirm the step themselves.

The practical reading rule: read array_equal as yes only when every entry matches and confirm the length of both lists first.

Academia Master Trader explains numerical tools with verifiable examples. More learning materials are available on academiamastertrader.com.br and via Aprenda.