Is there a way to know if the lists inside a list contain the same elements with python?
For example:
Return True if given list [['A', 'B'], ['A', 'B'], ['A', 'B']] or False if given list [['B', 'C'], ['Z', 'C']]
Is there a way to know if the lists inside a list contain the same elements with python?
For example:
Return True if given list [['A', 'B'], ['A', 'B'], ['A', 'B']] or False if given list [['B', 'C'], ['Z', 'C']]
You can write a loop that looks at each element, one at a time, and sees if it appears as any other element in the array.
To see if all entries in a list are unique you could do
def unique(lst):
d = []
for l in lst:
if not l in d:
d.append(l)
return len(d) == len(l)
This will check if all items are unique. If you want to see if all items are the same then check for length of one. For non-nested lists you would typically use a set but lists are not hashable so I'd use a loop. There may be a more pythony way of doing this but I'm not a python pro.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.