This is probably quite a straightforward question, but I can\'t find an answer elsewhere so I\'ll ask. What is the best way to find the number of times an element appears in
Use a nested generator expression:
Counter(x for sublist in my_list for x in sublist)
To count the items in the first position, a different generator expression gets that item for counting:
Counter(sublist[0] for sublist in my_list)
Demo:
>>> from collections import Counter
>>> my_list=[['a','b','c','d'],['a','b','z','d'],['a','c','f','e'],['d','w','f','a']]
>>> Counter(x for sublist in my_list for x in sublist)
Counter({'a': 4, 'd': 3, 'c': 2, 'b': 2, 'f': 2, 'e': 1, 'w': 1, 'z': 1})
>>> Counter(sublist[0] for sublist in my_list)
Counter({'a': 3, 'd': 1})