[Solved] How to convert a list to dict using python [closed]


A dictionary comprehension can do this:

{item[0]: item[1:] for item in inputlist}

As your input elements are tuples, your output values are tuples too:

>>> inputlist = [(u'name1', (47.5320299939, 7.70498245944), (47.5321349987, 7.70499587048), (47.5319710886, 7.70484834899), (47.5320299939, 7.70498245944)),(u'name2', (47.5320299939, 7.70498245944), (47.5321349987, 7.70499587048), (47.5319710886, 7.70484834899), (47.5320299939, 7.70498245944))]
>>> {item[0]: item[1:] for item in inputlist}
{u'name2': ((47.5320299939, 7.70498245944), (47.5321349987, 7.70499587048), (47.5319710886, 7.70484834899), (47.5320299939, 7.70498245944)), u'name1': ((47.5320299939, 7.70498245944), (47.5321349987, 7.70499587048), (47.5319710886, 7.70484834899), (47.5320299939, 7.70498245944))}
>>> pprint({item[0]: item[1:] for item in inputlist})
{u'name1': ((47.5320299939, 7.70498245944),
            (47.5321349987, 7.70499587048),
            (47.5319710886, 7.70484834899),
            (47.5320299939, 7.70498245944)),
 u'name2': ((47.5320299939, 7.70498245944),
            (47.5321349987, 7.70499587048),
            (47.5319710886, 7.70484834899),
            (47.5320299939, 7.70498245944))}

1

solved How to convert a list to dict using python [closed]