1

I am looking to convert the a list of 2-element lists to a dictionary. Note that I do not want to use group_by that has different outcomes than a simple conversion to dict. Is this possible? The two most obvious ways to try it out are not supported:

d = { x for x in [[1,2],[3,4]]}

Which gives us:

TypeError: unhashable type: 'list'

d = { *x for x in [[1,2],[3,4]]}

Which results in :

SyntaxError: iterable unpacking cannot be used in comprehension

yatu
  • 86,083
  • 12
  • 84
  • 139
WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560

1 Answers1

1

You should do:

d = { x: y for x, y in [[1,2],[3,4]]}

Output

{1: 2, 3: 4}

As suggested by @DeepSpace you could do:

dict([[1,2],[3,4]])
Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76