-3

I have the following python list of lists:

[[14.76, u'2017-10-11T06:00:00Z'], [14.75, u'2017-10-11T07:00:00Z'], [14.21, u'2017-10-11T08:00:00Z']]

And I need to get it into a dictionary:

{'2017-10-11T06:00:00Z' : 14.76, '2017-10-11T07:00:00Z':14.75, '2017-10-11T08:00:00Z': 14.21}
warrenfitzhenry
  • 2,209
  • 8
  • 34
  • 56
  • so, you forgot to *ask a question*. (A question is a sentence that rightfully ends in "?") What is it? Have you read the official python documentation on the dictionary type? – Marcus Müller Oct 15 '17 at 14:54

2 Answers2

2
d = dict()
for v, k in l:
    d[k] = v

l - initial list

ingvar
  • 4,169
  • 4
  • 16
  • 29
2

That's pretty simple. The dict constructor already accepts an iterable of two-element-iterables. The only extra difficulty here is that your keys and values are in reverse order.

>>> mylist = [[14.76, u'2017-10-11T06:00:00Z'], [14.75, u'2017-10-11T07:00:00Z'], [14.21, u'2017-10-11T08:00:00Z']]
>>> dict(map(reversed, mylist))
{u'2017-10-11T07:00:00Z': 14.75, u'2017-10-11T06:00:00Z': 14.76, u'2017-10-11T08:00:00Z': 14.21}*
timgeb
  • 76,762
  • 20
  • 123
  • 145