Problems: Unhashable List

I am making a function that translates normal strings into Braille characters.
To make the Braille dictionary, I create an empty braille_dictionary dictionary and then 2 functions addNumbers() and addLetters() to add keys and values to that dictionary:

def addNumbers(bd):
    bd['1'] = [1, 0, 0, 0, 0, 0]
    bd['2'] = [1, 1, 0, 0, 0, 0]
    bd['3'] = [1, 0, 0, 1, 0, 0]
    bd['4'] = [1, 0, 0, 1, 1, 0]
    bd['5'] = [1, 0, 0, 0, 1, 0]
    bd['6'] = [1, 1, 0, 1, 0, 0]
    bd['7'] = [1, 1, 0, 1, 1, 0]
    bd['8'] = [1, 1, 0, 0, 1, 0]
    bd['9'] = [0, 1, 0, 1, 0, 0]
    bd['0'] = [0, 1, 0, 1, 1, 0]


def addLetters(bd):
    bd['a'] = [1, 0, 0, 0, 0, 0]
    bd['b'] = [1, 1, 0, 0, 0, 0]
    bd['c'] = [1, 0, 0, 1, 0, 0]
    bd['d'] = [1, 0, 0, 1, 1, 0]
    bd['e'] = [1, 0, 0, 0, 1, 0]
    bd['f'] = [1, 1, 0, 1, 0, 0]
    bd['g'] = [1, 1, 0, 1, 1, 0]
    bd['h'] = [1, 1, 0, 0, 1, 0]
    bd['i'] = [0, 1, 0, 1, 0, 0]
    bd['j'] = [0, 1, 0, 1, 1, 0]

    bd['k'] = [1, 0, 1, 0, 0, 0]
    bd['l'] = [1, 1, 1, 0, 0, 0]
    bd['m'] = [1, 0, 1, 1, 0, 0]
    bd['n'] = [1, 0, 1, 1, 1, 0]
    bd['o'] = [1, 0, 1, 0, 1, 0]
    bd['p'] = [1, 1, 1, 1, 0, 0]
    bd['q'] = [1, 1, 1, 1, 1, 0]
    bd['r'] = [1, 1, 1, 0, 1, 0]
    bd['s'] = [0, 1, 1, 1, 0, 0]
    bd['t'] = [0, 1, 1, 1, 1, 0]

    bd['u'] = [1, 0, 1, 0, 0, 1]
    bd['v'] = [1, 1, 1, 0, 0, 1]
    bd['x'] = [1, 0, 1, 1, 0, 1]
    bd['y'] = [1, 0, 1, 1, 1, 1]
    bd['z'] = [1, 0, 1, 0, 1, 1]
    bd['w'] = [0, 1, 0, 1, 1, 1]

Here is my translator function:

def translator(string,braille_dict):
    translated = []
    wordList = string.split()
    for i in wordList:
        if i.lower() in braille_dict:
            i = braille_dict[i]
            translated.append(i)
        if i.lower() not in braille_dict:
            lst = []
            for j in i:
                if j in braille_dict:
                 j = braille_dict[j]
                 translated.append(j)
                if j not in braille_dict:
                 j = []
                 translated.append(j)
    return translated

The problem is that my function has done well with numbers and letters. For punctuation like question marks, I want to ignore, but my code returns the error: unhashable type: ‘list’
I will appreciate if you could help me solve this bug!

It looks like if j is in braille_dict, you change j to be a list
and then check if j (which may now be a list) is not in braille_dict
but that gives an error because a list can’t be a key for a dictionary.

To fix this, change the second if to an elif
(or I’d go with an else)
so that changing j in the previous if-statement block does not affect this block.

I changed it to elif but it still returns the unhashable error

Hey it worked!! Thank you so much!

I changed the j being used for the list to a k and it seems to work (except for spaces).

def addNumbers(bd): bd['1'] = [1, 0, 0, 0, 0, 0] bd['2'] = [1, 1, 0, 0, 0, 0] bd['3'] = [1, 0, 0, 1, 0, 0] bd['4'] = [1, 0, 0, 1, 1, 0] bd['5'] = [1, 0, 0, 0, 1, 0] bd['6'] = [1, 1, 0, 1, 0, 0] bd['7'] = [1, 1, 0, 1, 1, 0] bd['8'] = [1, 1, 0, 0, 1, 0] bd['9'] = [0, 1, 0, 1, 0, 0] bd['0'] = [0, 1, 0, 1, 1, 0] def addLetters(bd): bd['a'] = [1, 0, 0, 0, 0, 0] bd['b'] = [1, 1, 0, 0, 0, 0] bd['c'] = [1, 0, 0, 1, 0, 0] bd['d'] = [1, 0, 0, 1, 1, 0] bd['e'] = [1, 0, 0, 0, 1, 0] bd['f'] = [1, 1, 0, 1, 0, 0] bd['g'] = [1, 1, 0, 1, 1, 0] bd['h'] = [1, 1, 0, 0, 1, 0] bd['i'] = [0, 1, 0, 1, 0, 0] bd['j'] = [0, 1, 0, 1, 1, 0] bd['k'] = [1, 0, 1, 0, 0, 0] bd['l'] = [1, 1, 1, 0, 0, 0] bd['m'] = [1, 0, 1, 1, 0, 0] bd['n'] = [1, 0, 1, 1, 1, 0] bd['o'] = [1, 0, 1, 0, 1, 0] bd['p'] = [1, 1, 1, 1, 0, 0] bd['q'] = [1, 1, 1, 1, 1, 0] bd['r'] = [1, 1, 1, 0, 1, 0] bd['s'] = [0, 1, 1, 1, 0, 0] bd['t'] = [0, 1, 1, 1, 1, 0] bd['u'] = [1, 0, 1, 0, 0, 1] bd['v'] = [1, 1, 1, 0, 0, 1] bd['x'] = [1, 0, 1, 1, 0, 1] bd['y'] = [1, 0, 1, 1, 1, 1] bd['z'] = [1, 0, 1, 0, 1, 1] bd['w'] = [0, 1, 0, 1, 1, 1] def translator(string,braille_dict): translated = [] wordList = string.split() for i in wordList: if i.lower() in braille_dict: i = braille_dict[i] translated.append(i) if i.lower() not in braille_dict: lst = [] for j in i: if j in braille_dict: k = braille_dict[j] translated.append(k) else: k = [] translated.append(k) return translated braille_dictionary = {} addNumbers(braille_dictionary) addLetters(braille_dictionary) sentence = "Do you want to go to the mall today?" translated = translator(sentence, braille_dictionary) for letter in translated: print (letter)
1 Like