-2

I am trying to turn a nested list like this: (Example list)

[['A','1','2','3'], ['B','4','5','6'],...]

To a dictionary that looks like this

{'A':'1','2','3','B': '4','5','6'}

Can someone please help me?

1 Answers1

0

You can use dictionary comprehension:

lst = [['A','1','2','3'], ['B','4','5','6']]
{e[0]: e[1:] for e in lst}

This returns:

{'A': ['1', '2', '3'], 'B': ['4', '5', '6']}
Flux
  • 9,805
  • 5
  • 46
  • 92
  • Is there a way I can unpack the values? I don't want to keep the values as a list – Triet Nguyen Jan 31 '20 at 04:36
  • 2
    @TrietNguyen What do you mean by "unpack"? Note that the example in your question (i.e. `{'A': '1','2','3','B': '4','5','6'}`) is unachievable because it is invalid Python syntax. That's not how dictionaries in Python work. Try entering `{'A':'1','2','3','B': '4','5','6'}` in your Python interpreter. You'll just get `SyntaxError: invalid syntax`. – Flux Jan 31 '20 at 04:49