Comparing list in python
Question: Hey im starting to learn python and after a loop function i got a list which
looks like this

    
    
    >>>print test
    
    ['a','b','c','d']
    ['a','c','d','e']
    ['b','d','e','f']
    

I want to compare it and found things like intersection BUT as the two lists
are under one name i can't get it done.

I want to compare the lists and count the letters. Expectation:

    
    
    d, 3
    a, 2
    b, 2
    

etc

Someone can help me with that?

best regards


Answer: If you have list of lists, then you can use `collections.Counter` to count
letters:

    
    
    >>> from collections import Counter
    >>> test = [['a','b','c','d'],['a','c','d','e'],['b','d','e','f']]
    >>> Counter(i for x in test for i in x)
    Counter({'d': 3, 'a': 2, 'c': 2, 'b': 2, 'e': 2, 'f': 1})
    

or, as inspectorG4dget suggested in comments, use
`itertools.chain.from_iterable`:

    
    
    >>> import itertools
    >>> from collections import Counter
    >>> test = ['test','test2']
    >>> Counter(itertools.chain.from_iterable(test))
    Counter({'t': 4, 's': 2, 'e': 2, '2': 1})
    

