Menu

gitpiper

have_same_contents python Code Snippet in 2024

listintermediate

Last Updated: 24 March 2024

Checks if two lists contain the same elements regardless of order.

  • Use set() on the combination of both lists to find the unique values.
  • Iterate over them with a for loop comparing the count() of each unique value in each list.
  • Return False if the counts do not match for any element, True otherwise.
def have_same_contents(a, b): for v in set(a + b): if a.count(v) != b.count(v): return False return True
have_same_contents([1, 2, 4], [2, 4, 1]) # True

python snippet similar to have_same_contents For You in March 2024

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2024 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️