Python dictionary keys being separated into characters, why?

59 views Asked by At

I'm fairly new to python and I'm getting a problem when trying to declare a dictionary, my code is

d = {}
d["hi"] = "1"

Which returns

{'h':'1', 'i':'1'}

Instead of

{'hi':'1'}

Why does this happen and how can I fix this?

1

There are 1 answers

3
Yohannes On BEST ANSWER

You can use d.update({'hi':'1', 'hola':'2'}) to update multiple keys at once.

When updating a single key its around 3x faster to update the item inplace, ie:

d['hi']='1'

This is explained in here: python dict.update vs. subscript to add a single key/value pair