-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}
1
  • 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? Commented Oct 15, 2017 at 14:54

2 Answers 2

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}*
Sign up to request clarification or add additional context in comments.

Comments

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

l - initial list

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.