DEV Community

Ujjwal Kumar Singh
Ujjwal Kumar Singh

Posted on

Python for SDETs: Comparing Two API Response Arrays

Ever got two API responses and needed to know what matched?
Classic scenario — your API returns a list of user IDs. Your expected list has what should be there. You need to know what actually matched and how many.
Here is the Python pattern I use:

expected = [10, 12, 14, 15, 16, 28]
actual = [13, 14, 16, 17]
matched = []
count = 0
for i in expected:
    for j in actual:
        if i == j:
            count += 1
            matched.append(i)

print("Number of matching elements:", count)

print(matched)
Enter fullscreen mode Exit fullscreen mode

*Output *
Number of matching elements: 2
[14, 16]

Two values matched. Everything else is either missing or unexpected, both of which are bugs worth reporting.

Top comments (2)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.