All the ways to initialize a Python dictionary

All the ways to initialize a Python dictionary

1:
xtest = {"k1":"v1", "k2":"v2"}

2:
xtest = dict(k1="v1", k2="v2")

3:
xtest = {}
xtest["k1"] = "v1"
xtest["k2"] = "v2"

4:
xtest = dict.fromkeys(("k1", "k2"),("v1", "v2"))

5:
xtest = dict(zip(["k1", "k2"], ["v1", "v2"]))

6:
list = [("k1", "v1"), ("k2", "v2")]
xtest = dict(list)

The actual use of the initialization method according to their own situation, dict () and {} way compared, {} performance is better.

Comments

Popular posts from this blog

Python Receiving and parse JSON Data via UDP protocol

ubus lua client method and event registration code demo/example