Comparing List Contents in Programming

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In programming, it is often necessary to check if two lists contain the same elements, regardless of their order. This can be useful in a variety of situations, such as checking if two sets of data have the same values or verifying if a function is returning the expected output. In this challenge, you will write a function that checks if two lists have the same contents.

Check if Two Lists Have the Same Contents

Write a function have_same_contents(a, b) that takes two lists as arguments and returns True if they have the same contents, False otherwise. The function should check if the two lists contain the same elements, regardless of their order.

To solve this problem, you can follow these steps:

  1. Use set() on the combination of both lists to find the unique values.
  2. Iterate over them with a for loop comparing the count() of each unique value in each list.
  3. 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

Summary

In this challenge, you have learned how to check if two lists have the same contents, regardless of their order. By using the set() function and iterating over the unique values, you can compare the count() of each element in both lists to determine if they have the same contents.

Other Python Tutorials you may like