1

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

dict = {}
dict['key' : 'value']
print(dict)
Community
  • 1
  • 1
mike
  • 21
  • 2

3 Answers3

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

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

Shivam Seth
  • 677
  • 1
  • 8
  • 21
0

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

shyam_gupta
  • 309
  • 2
  • 11
0

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']