开发者

Oneline population of dictionary from array in Python

I need to populate dictionary from array开发者_如何学JAVA. I've done in three lines and i'm trying to do it shortest as it can be. Is there way how to populate it in single line?

a = [['test',154],['test2',256]]
d = dict()
for b in a:
    d[b[0]] = b[1]


Just dict :)

>>> a = [['test',154],['test2',256]]
>>> dict(a)
{'test': 154, 'test2': 256}


You just do dict(a) or dict([['test',154],['test2',256]]).


L = [['test',154],['test2',256]]

In Python 3.x:

d = {k:v for k,v in L}

In Python 2.x:

d = dict([(k,v) for k,v in L])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜