Python - Using set to perform order-neutral equality tests

Introduction

You can use sets to perform order-neutral equality tests by converting to a set before the test.

Two sets are equal if and only if every element of each set is contained in the other.

Demo

L1, L2 = [1, 3, 5, 2, 4], [2, 5, 3, 4, 1] 
print( L1 == L2 )                                          # Order matters in sequences 
print( set(L1) == set(L2) )                                # Order-neutral equality 
print( sorted(L1) == sorted(L2) )                          # Similar but results ordered 
print( 'test' == 'asmp', set('test') == set('asmp'), sorted('test') == sorted('asmp') )

Result

Related Topic