3

So I've got tuples inside of tuples, and I would like to turn them into a key: value pair.

((1L, 'I.T.'), (2L, 'Project Management'), (3L, 'Creative'), (4L, 'Programming'), (5L, 'Sales'), (6L, 'Administration'), (7L, 'AV'), (8L, 'Human Resources'), (9L, 'Conference Rooms'), (10L, 'Testing'), (11L, 'none'))

How would I go about doing this?

Steven Matthews
  • 9,705
  • 45
  • 126
  • 232
  • possible duplicate of [Transform tuple to dict](http://stackoverflow.com/questions/3553949/transform-tuple-to-dict) and [List of tuples to dictionary](http://stackoverflow.com/questions/6522446/python-list-of-tuples-to-dictionary) – Felix Kling Nov 01 '11 at 19:49

2 Answers2

11

Just pass it to the dict constructor/function! It can take any iterable of (key, value) tuples and create a dictionary from it.

>>> x = ((1L, 'I.T.'), (2L, 'Project Management'), (3L, 'Creative'), (4L, 'Programming'), (5L, 'Sales'), (6L, 'Administration'), (7L, 'AV'), (8L, 'Human Resources'), (9L, 'Conference Rooms'), (10L, 'Testing'), (11L, 'none')
>>> dict(x)
{1L: 'I.T.', 2L: 'Project Management', 3L: 'Creative', 4L: 'Programming', 5L: 'Sales', 6L: 'Administration', 7L: 'AV', 8L: 'Human Resources', 9L: 'Conference Rooms', 10L: 'Testing', 11L: 'none'}
Jeremy
  • 1
  • 85
  • 340
  • 366
2

By calling dict with your tuple as an argument:

d = dict(t)
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452