Need to try and count repeated lists within a list
Question: Im trying to count how many repeated lists there are inside a list. But it
doesnt work the same way I could count repeated elements in just a list. Im
fairly new to python, so apologies if it sounds too easy.

this is what i did

    
    
    x=  [["coffee", "cola", "juice" "tea" ],["coffee", "cola", "juice" "tea"]
    ["cola", "coffee", "juice" "tea" ]]
    dictt= {}
    
    for item in x:
    
        dictt[item]= dictt.get(item, 0) +1
    
    return(dictt)
    


Answer: Your code **_almost_** works. As others have mentioned, lists cannot be used
as dictionary keys but tuples can. The solution is to turn each list into a
tuple.

    
    
    >>> x=  [["coffee", "cola", "juice", "tea"], ### <-- this list appears twice
    ...      ["coffee", "cola", "juice", "tea"],
    ...      ["cola", "coffee", "juice", "tea"]] ### <-- this list appears once
    >>> 
    >>> dictt= {}
    >>> 
    >>> for item in x:
    ...     # turn the list into a tuple
    ...     key = tuple(item)
    ...
    ...     # use the tuple as the dictionary key
    ...     # get the current count for this key or 0 if the key does not yet exist
    ...     # then increment the count
    ...     dictt[key]= dictt.get(key, 0) + 1
    ... 
    >>> dictt
    {('cola', 'coffee', 'juice', 'tea'): 1, ('coffee', 'cola', 'juice', 'tea'): 2}
    >>> 
    

You can turn the tuples back into lists if you need to.

    
    
    >>> for key in dictt:
    ...     print list(key), 'appears ', dictt[key], 'times'
    ... 
    ['cola', 'coffee', 'juice', 'tea'] appears  1 times
    ['coffee', 'cola', 'juice', 'tea'] appears  2 times
    >>> 
    

In addition, Python has a collections.Counter() class which is designed
specifically for counting things. (NOTE: You will still need to turn the lists
into tuples.)

    
    
    >>> from collections import Counter
    >>> counter = Counter()
    >>> for item in x:
    ...    counter[tuple(item)] += 1
    ... 
    >>> counter
    Counter({('coffee', 'cola', 'juice', 'tea'): 2, ('cola', 'coffee', 'juice', 'tea'): 1})
    >>> 
    

Counter() is a subclass of dict(), so all the dictionary methods still work.

    
    
    >>> counter.keys()
    [('coffee', 'cola', 'juice', 'tea'), ('cola', 'coffee', 'juice', 'tea')]
    >>> k = counter.keys()[0]
    >>> k
    ('coffee', 'cola', 'juice', 'tea')
    >>> counter[k]
    2
    >>> 
    

