1

From this question:

>>> t_list = [('a', 10, 98), ('b', 234, 65), ('c', 459, 5)]
>>> t_dict = {a:{b:c} for a,b,c in t_list}
>>> t_dict
{'a': {10: 98}, 'c': {459: 5}, 'b': {234: 65}}

And I can call values based on the first element such as:

>>> t_dict['a']
{10: 98}

But how would I access individual values based on the key? Such as 10 or 98. I am expecting something like this: t_dict['a'][0]. I have tried using split() and slicing it but no luck.

cptpython
  • 393
  • 2
  • 5
  • 12

2 Answers2

4

Use this instead:

t_dict = {a:[b,c] for a,b,c in t_list}

Since you wanted to save b and c as a list.

Taku
  • 31,927
  • 11
  • 74
  • 85
  • Works brilliantly! Thanks! Originally, I wanted to save b and c as values which reference to `dict keys` i.e. `'a', 'b', 'c'`. – cptpython Mar 22 '17 at 20:41
0

The generic solution would be this one:

t_dict = {t[0]: t[1:] for t in t_list}

And if you consider the particular case you'd have:

t_dict = {t[0]:t[1:3] for t in t_list}
BPL
  • 9,632
  • 9
  • 59
  • 117