Nested List To Nested Dictionary In Python
I have what seems to be a tricky question, but probably quite a simple solution. I have a nested list that I wish to have the first value of every list contained to be the key with
Solution 1:
>>> stations = [[1, 'carnegie', 12345, 30],[2, 'london', 12344, 30], [4, 'berlin', 1345563, 30]]
You can use a dict
comprehension
>>> {i[0] : i[1:] for i in stations}
{1: ['carnegie', 12345, 30], 2: ['london', 12344, 30], 4: ['berlin', 1345563, 30]}
Solution 2:
Use a dict
comprehension:
{i[0]: i[1:]foriinstations}
Post a Comment for "Nested List To Nested Dictionary In Python"