XXX
XXX

Reputation: 57

Python : Use a list as dictionary key

I'm sure the solution to my issue is pretty simple but I couldn't find it out.

When running this, I'm getting

TypeError: unhashable type: 'list'

which is expected since I'm working with a list:

import operator
import shutil

def start(source):
    source=open('Book.txt', 'r', encoding='UTF-8')
    wordslist=[]
    for word in source:
        content=word.lower().split()
        for each_word in content:
            #print(each_word)
            wordslist.append(content)

    #cleanuptext(wordslist)
    sortdictionnary(wordslist)
'''
def cleanuptext(wordslist):
    cleanwords=[]
    for cleanword in wordslist:
        symbols=',.'
        for i in range(0, len(symbols)):
            cleanword = str(cleanword).replace(symbols[i], "")
            if len(cleanword) > 0:
                print(cleanword)
                cleanwords.append(cleanword)
'''
def sortdictionnary(wordslist):
    counter={}
    for word in wordslist:
        if word in counter:
            counter[word] += 1
        else:
            counter[word] = 1
    for key, value in sorted(counter.items(), key=operator.itemgetter(0)):
        print(key, value)
start(source='Book.txt')

How can I avoid this problem and be able to use my list in my dictionary?

Eventhough I tried to use Counter, I got the same issue :

import operator
import shutil
from collections import Counter


def start(source):
    source=open('Book.txt', 'r', encoding='UTF-8')
    wordslist=[]
    for word in source:
        content=word.lower().split()
        for each_word in content:
            #print(each_word)
            wordslist.append(content)

    #cleanuptext(wordslist)
    sortdictionnary(wordslist)
'''
def cleanuptext(wordslist):
    cleanwords=[]
    for cleanword in wordslist:
        symbols=',.'
        for i in range(0, len(symbols)):
            cleanword = str(cleanword).replace(symbols[i], "")
            if len(cleanword) > 0:
                print(cleanword)
                cleanwords.append(cleanword)
'''
def sortdictionnary(wordslist):
    coun = Counter()
    for word in wordslist:
        if word in coun:
            coun[word] += 1
        else:
            coun[word] = 1
    for key, value in sorted(coun.items(), key=operator.itemgetter(0)):
        print(key, value)
start(source='Book.txt')

Thanks and regards

Upvotes: 1

Views: 103

Answers (1)

6502
6502

Reputation: 114461

One problem is that list are mutable and you cannot use a mutable value as key in a dictionary. An alternative is to use a tuple

content = tuple(word.lower().split())

To understand the reason Python doesn't want mutable values as keys consider that you put two different keys a and b in a dictionary with two values, and then you mutate the mutable a (the first key) so that it becomes equal to b... what should happen to the dictionary? In a Python dictionary there cannot be two equal keys with distinct values.

Tuples are ok as keys because they're similar to lists but immutable.

Upvotes: 1

Related Questions