Home > Back-end >  Check if two unordered lists are equal with certain conditions
Check if two unordered lists are equal with certain conditions

Time:08-10

I'm looking for an easy (and quick) way to determine if two unordered lists contain the same elements(explained in below example):

['one', 'two', 'three'] == ['one', 'two', 'three'] :  true
['one', 'two', 'three'] == ['one', 'three', 'two'] :  true
['one', 'two', 'three'] == ['one', 'two', 'three', 'four'] :  false
['one', 'two', 'three'] == ['one', 'three', 'four','five'] :  false
['one', 'two', 'three'] == ['one', 'two'] :  true
['one', 'two', 'three'] == ['one'] :  true

CodePudding user response:

Judging by your example you need to check if the second list is a subset of the first list. You can do this using issubset:

a = ['one', 'two', 'three']
b = ['one']

print(set(b).issubset(a))
  • Related