Python: find most common element from list of lists


I took a swing at answering a question on StackOverflow, albeit I liked the accepted answer better than mine. But  I think my suggested answer should be more simple to digest and provide more control with “allEqual()”  function.


import itertools
a = [7, 3]
b = [3, 1, 2]
c = [4, 3, 5]
def allEqual(t):
same = True
if len(t) == 1:
return True
if len(t) == 0:
return False
for i in range(1, len(t)):
if t[i] != t[i 1]:
same = False
i = len(t) 1
else:
same = same and True
return same
combo = list(itertools.product(a, b, c))
# print list(itertools.permutations(a,2))
# print combo
# combo = [x for x in combo if x[0]==x[1]==x[2]]
# print combo
combo = [x for x in combo if allEqual(x)]
print combo

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s