Home > Net >  Getting type error when modifying the values in a dictionary
Getting type error when modifying the values in a dictionary

Time:12-22

I've made the following dictionary:

client_dict = {'client 1':['ABC', 'EFG'], 'client 2':['MNO','XYZ'], 'client 3':['ZZZ']}

I want to: get input from the user, show the value of the client and if ok keep the current state of the dictionary and if not the user can change the values for the given client. To do this I made the following:

x = client_dict[input('Enter the client name:\n')]
print(x)
y = input('if ok enter y otherwise enter n:\n')
if y =='n':
    lst = []
    for i in range(len(x)):
        x[i] = input('enter the correct header:\n')
        lst.append(x[i])
    client_dict[x] = lst
else: 
    pass

Suppose in first input I input client 1 and then enter n meaning I want to change values. Then, the algorithm ask me twice to enter the desired header (as client 1 has two values), for the first header I write hello, and for the second I write world. The line up would be as follow:

Enter the client name:
client 1
['ABC', 'EFG']
if ok enter y otherwise enter n:
n
enter the correct header:
hello
enter the correct header:
world 

I can now check my client_dict which is modified to:

 {'client 1': ['hello', 'world'],
 'client 2': ['MNO', 'XYZ'],
 'client 3': ['ZZZ']}

which means the code DOES what I want, but when the process is over in the conditional statement, I also get the following error:

TypeError: unhashable type: 'list'

coming from this : client_dict[x] = lst. So I wonder what am I doing wrong? Despite the fact that the code works, it seems there is some issue when over writing the dictionary?

CodePudding user response:

I changed your code, try this:

client_dict = {'client 1':['ABC', 'EFG'], 'client 2':['MNO','XYZ'], 'client 3':['ZZZ']}

x = input('Enter the client name:\n')
print(client_dict[x])
y = input('if ok enter y otherwise enter n:\n')
if y == 'n':
    for i in range(len(client_dict[x])):
        client_dict[x][i] = input('enter the correct header:\n')
else: 
    pass

Sorry, try this now. Edited

CodePudding user response:

I think you are accidentally assigning the value to the key in client_dict. As in, you are trying to say a value, the List, is the key in a new entry on the line it errors on. You probably wanted to do something like:

client_dict["client1"] = x but replace "client1" with a variable that represents that clients name. You probably mistakenly thought that x was the name of your client (aka, the key), but its actually equal to the value of this dictionary entry, because of this line:

x = client_dict[input('Enter the client name:\n')] This is saying "Assign x to the resulting value when I go into dictionary "client_dict" and access the spot where key (result of input() call) resides"

It would probably help to write out what your dictionary is meant to be by kvp:

Keys should be: strings

Values should be: lists

then, go through your code and think about what type everything is. This is a problem with Python as its easy to mix up what type each variable was supposed to represent since its dynamically typed

  • Related