How to Compare Two Arrays in Python
Use sets for membership checks, loops for positional diffs, and a visual browser diff when the data stops being simple.
Python gives you several ways to compare lists, but the right approach depends on what you mean by compare. Sometimes you only care about missing values. Sometimes order matters. Sometimes the list contains dicts and nested structures where a set operation stops being useful.
1. Compare membership with set difference
If you only care about which values appear in one list but not the other, convert both lists to sets and compare the membership results.
left = ['flask', 'django', 'requests', 'numpy']
right = ['flask', 'fastapi', 'requests', 'pandas'] removed = set(left) - set(right)
added = set(right) - set(left)This is fast and clear, but it drops ordering and duplicates. If ['a', 'a', 'b'] matters to you, a set is the wrong tool.
2. Compare by position when order matters
Use a loop when index position is part of the meaning of the data. This is common when you are checking pipeline output, sorted reports, or arrays that are expected to stay stable between runs.
for i in range(max(len(left), len(right))): before = left[i] if i < len(left) else '(missing)' after = right[i] if i < len(right) else '(missing)' if before != after: print(i, before, after)3. Nested lists and dicts need a deeper comparison
Once the list contains dicts, nested arrays, or mixed types, a manual loop becomes noisy. You can serialize the data or use a library like DeepDiff, but for quick inspection a browser-based visual diff is faster because it shows every added, removed, and modified path side by side.
When to switch to ArrayDiff
Use Python List Diff when you need a full visual explanation instead of a boolean or a pair of sets. It is especially useful for ETL debugging, comparing exported DataFrame rows, and reviewing JSON-serializable Python objects from test fixtures.
If your Python data is already JSON-shaped and you care about keys inside dicts, go directly to Python JSON Diff or the broader Array Comparison Tools hub.