I'm trying to modify a formatted list in a formatted [["bill", 21], ["kevin", 42], ["gail",20]] dictionary {"bill":21, "kevin":42, "gail":20} . Is there an easy way to do this?
[["bill", 21], ["kevin", 42], ["gail",20]]
{"bill":21, "kevin":42, "gail":20}
In [2]: l = [['bill', 21], ['kevin', 42], ['gail',20]] In [3]: d = dict(l) In [4]: d Out[4]: {'gail': 20, 'bill': 21, 'kevin': 42}
The constructor dict() accepts a two-size iterable iteration. This way you can just call dict on your list:
dict()
dict
In [11]: L = [['bill', 21], ['kevin', 42], ['gail',20]] In [12]: dict(L) Out[12]: {'bill': 21, 'kevin': 42, 'gail': 20}
To convert dictionary values to integers use this code:
>>> text=[["bill", '21'], ["kevin", '42'], ["gail",'20']] >>> dict([[i,int(j)] for i,j in text]) {'bill': 21, 'kevin': 42, 'gail': 20}