python tuple to dict
--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Breezy Bay
--
Chapters
00:00 Python Tuple To Dict
00:21 Accepted Answer Score 366
00:31 Answer 2 Score 96
00:41 Answer 3 Score 17
00:53 Answer 4 Score 54
01:02 Thank you
--
Full question
https://stackoverflow.com/questions/3783...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #dictionary #tuples
#avk47
    Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Breezy Bay
--
Chapters
00:00 Python Tuple To Dict
00:21 Accepted Answer Score 366
00:31 Answer 2 Score 96
00:41 Answer 3 Score 17
00:53 Answer 4 Score 54
01:02 Thank you
--
Full question
https://stackoverflow.com/questions/3783...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #dictionary #tuples
#avk47
ACCEPTED ANSWER
Score 366
Try:
>>> t = ((1, 'a'),(2, 'b'))
>>> dict((y, x) for x, y in t)
{'a': 1, 'b': 2}
ANSWER 2
Score 96
A slightly simpler method:
>>> t = ((1, 'a'),(2, 'b'))
>>> dict(map(reversed, t))
{'a': 1, 'b': 2}
ANSWER 3
Score 54
Even more concise if you are on python 2.7:
>>> t = ((1,'a'),(2,'b'))
>>> {y:x for x,y in t}
{'a':1, 'b':2}
ANSWER 4
Score 17
>>> dict([('hi','goodbye')])
{'hi': 'goodbye'}
Or:
>>> [ dict([i]) for i in (('CSCO', 21.14), ('CSCO', 21.14), ('CSCO', 21.14), ('CSCO', 21.14)) ]
[{'CSCO': 21.14}, {'CSCO': 21.14}, {'CSCO': 21.14}, {'CSCO': 21.14}]