Suppose I have
data =
[[a, a, c],
[b, c, c],
[c, b, b],
[b, a, c]]
I want to get a list containing the element that occurs the most in each
Use a list comprehension plus collections.Counter():
from collections import Counter
[Counter(col).most_common(1)[0][0] for col in zip(*data)]
zip(*data)
rearranges your list of lists to become a list of columns instead. Counter()
objects count how often anything appears in the input sequence, and .most_common(1)
gives us the most popular element (plus it's count).
Provided your input is single character strings, that gives:
>>> [Counter(col).most_common(1)[0][0] for col in zip(*data)]
['b', 'a', 'c']