w3resource

Python Data Structure: Compare two unordered lists

Python Data Structure: Exercise-11 with Solution

Write a Python program to compare two unordered lists (not sets).

Sample Solution:

Python Code :

from collections import Counter
def compare_lists(x, y):
    return Counter(x) == Counter(y)
n1 = [20, 10, 30, 10, 20, 30]
n2 = [30, 20, 10, 30, 20, 50]
print(compare_lists(n1, n2))

Sample Output:

False

Flowchart:

Flowchart: Compare two unordered lists

Python Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Python program to group a sequence of key-value pairs into a dictionary of lists.
Next: Write a Python program to create an array contains six integers. Also print all the members of the array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.