Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting type error when modifying the values in a dictionary

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading