Meaning
This pattern asserts that two lists contain the same elements regardless of order by sorting both lists and comparing them for equality. It is commonly used in tests where the order of items is irrelevant or nondeterministic. Sorting provides a simple way to achieve multiset equality when elements are sortable.
Primary Function
Testing / Assertion
Communicative Purpose
Ensures unordered equality of two sequences.
Pattern
assert sorted(actual_list) == sorted(expected_list)
Core Structure
assert sorted(...) == sorted(...)
Função primária
Testing / Assertion
Propósito comunicativo
Ensures unordered equality of two sequences.
Situações de gatilho
Unit testing: verifying function output where order is not guaranteed; Algorithm verification: comparing results with nondeterministic ordering; Data validation: checking fixtures against expected unordered data
Contextos
Unit test suites, pytest, doctest, data processing pipelines, algorithm verification.
Padrão
assert sorted(actual_list) == sorted(expected_list)
Estrutura central
assert sorted(...) == sorted(...)
Slots de substituição
actual_list: iterable, expected_list: iterable
Colocados típicos
- used in unit tests
- pytest assertions
- test fixtures
Substituições comuns
- could also use collections.Counter(actual_list) == collections.Counter(expected_list) or set(actual_list) == set(expected_list) if elements are hashable and unique
Erros comuns
1. Using assert without sorting can fail due to order differences; cause: forgetting that order matters in equality; consequence: false test failures when order differs. 2. Forgetting that sorting may be O(n log n) and not suitable for large lists; cause: assuming sorting is cheap; consequence: performance degradation on large datasets. 3. Assuming elements are sortable (must implement __lt__); cause: using custom objects without __lt__; consequence: TypeError at runtime. 4. Using set() for equality when duplicates matter; cause: ignoring duplicate counts; consequence: false positives when duplicates differ. 5. Assuming sorting creates no copies; cause: overlooking memory overhead; consequence: increased memory usage for large lists.
Similar / contraste
1. assert actual_list == expected_list: order-sensitive equality. 2. assert collections.Counter(actual_list) == collections.Counter(expected_list): multiset equality that respects duplicates and works for unhashable items if they are comparable. 3. assert set(actual_list) == set(expected_list): equality for unique elements only, ignores duplicates. 4. Using sorted with key function: assert sorted(actual_list, key=key_func) == sorted(expected_list, key=key_func): order‑insensitive equality with custom ordering. 5. Using frozenset for unordered comparison: assert frozenset(actual_list) == frozenset(expected_list): same as set but immutable.
Interferências
Coming from C: may forget that Python asserts can be disabled with -O → use explicit test framework assertions like self.assertEqual(sorted(actual), sorted(expected)).
Família do chunk
- assert sorted(...) == sorted(...)
- assert Counter(...) == Counter(...)
- assert set(...) == set(...)
Nuance
Do not use when elements are not comparable or when performance is critical for large data; sorting creates copies and is O(n log n), consider collections.Counter for large multisets; if elements are unhashable, provide a key function or convert to comparable tuples.
Efeito pragmático
Makes intent explicit that order is irrelevant; simplifies equality checks for unordered collections.
Dica de memória
Sort then compare to ignore order.
Nota
Only works if elements are comparable; sorting creates copies and is O(n log n); for large data consider collections.Counter; if elements are not sortable, use a custom key function or Counter for multisets.
Upgrade path
from collections import Counter; assert Counter(actual_list) == Counter(expected_list)
Log in to save chunks.