-6

i have a list looks like this

 [(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))]

i want to convert the list as dict something like this

{'name1': (47.5320299939, 7.70498245944), (47.5321349987, 7.70499587048), (47.5319710886, 7.70484834899), (47.5320299939, 7.70498245944)}

i want to do it in python. can anyone help.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

1 Answers1

1

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))}
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343