mike
mike

Reputation: 21

Python TypeError: unhashable type: 'slice' in dictionary

I am getting this TypeError in this simple code, appending a value to a dictionary.

dict = {}
dict['key' : 'value']
print(dict)

Upvotes: 1

Views: 3292

Answers (3)

Mayank Maheshwari
Mayank Maheshwari

Reputation: 303

You can't add like this, it's a dictionary that takes key and value as a separate entity like

dict['key'] = 'value'

After doing this, the dictionary will automatically convert itself like-

dict['key' : 'value']

Upvotes: 0

shyam_gupta
shyam_gupta

Reputation: 328

well you can do much more d={} for appending to it you can do

d={'key1':'value1','key2':'value'}

also

d.update(key3:'val3')

hope this helps

Upvotes: 0

Shivam Seth
Shivam Seth

Reputation: 687

dict = {}
dict['key'] = 'value'
print(dict)

[index1 :index2 ] is list slice operation, which will not work on dict

Upvotes: 2

Related Questions