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
  • 80,714
  • 11
  • 64
  • 111
WestCoastProjects
  • 53,260
  • 78
  • 277
  • 491

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
  • 55,057
  • 6
  • 42
  • 65