How to convert a nested list into a dictionary

Ella X

I am supposed to convert a nested list into a dictionary. If I have the list:

data=[[['1','2','-2'], ['3','-1','4']]

I want it to become:

d={(0, 0): 1, (0, 1): 3, (1, 0): 2, (1, 1): -1, (2, 0): -2, (2, 1): 4}

The tricky part is I you need to see this list as a matrix:

'1','2','-2'
'3','-1','4'
  • Note:Keys in d are not those values' position in that list. keys should be positions of values in that matrix. For example, the key of '3' is (0,1) while the position of '3' in the list is [1][0]. (0,1) should be column o and row 1 in that matrix(I understand it in this way)

So the keys in the dictionary should be their position in that matrix. I am confused and I tried:

for m in range(len(data)):
    for n in range(len(data[m])):
        d[(m,n)]= data[n][m]

I know it's wrong because it will become out of range. I am so struggling with it.

Ajax1234

Your current data is invalid syntax, however, I believe this is what you are looking for:

data= [['1','2','-2'], ['3','-1','4']]
new_result = {(a, i):data[i][a] for i in range(len(data)) for a in range(len(data[0]))}

Output:

{(0, 1): '3', (0, 0): '1', (2, 1): '4', (2, 0): '-2', (1, 0): '2', (1, 1): '-1'}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to turn a list into a nested dictionary?

From Dev

How to write a list with a nested dictionary to a csv file?

From Dev

How to convert tuple to a multi nested dictionary in python?

From Dev

Convert nested dictionary into dataframe

From Dev

python dictionary and list: how to convert it?

From Dev

How to store CSV data in a Nested Dictionary that has a dictionary and a list?

From Dev

How to convert a dictionary into a flat list?

From Dev

Convert nested class to dictionary

From Dev

How convert list of dictionaries to a dictionary of dictionaries in Python

From Dev

how to create a nested dictionary of list

From Dev

How to convert Dictionary to List with mixing of values and keys?

From Dev

How to convert this list into a dictionary

From Dev

Convert list of nested dictionary to a flat list of dictionary

From Dev

How to convert a list of tuples to a dictionary of dictionaries in Python?

From Dev

Flatten nested dictionary - convert list element to string

From Dev

Flatten nested dictionary - convert list element to string

From Dev

How to create a nested dictionary from a list in Python?

From Dev

How to convert multiple list to dictionary

From Dev

how to create a nested dictionary of list

From Dev

How to write a list with a nested dictionary to a csv file?

From Dev

How to user LINQ to convert list to dictionary?

From Dev

How to convert this dictionary into a list of tuples?

From Dev

How to extract from this list of tuples and convert into this dictionary?

From Dev

python dictionary and list: how to convert it?

From Dev

How to convert aspell dictionary to simple list of words?

From Dev

How to convert list of list to dictionary in Python?

From Dev

How to convert list into nested list in Haskell

From Dev

How to convert a nested list to a single list?

From Dev

How to convert nested dictionary to dataframe in Python

Related Related

  1. 1

    How to turn a list into a nested dictionary?

  2. 2

    How to write a list with a nested dictionary to a csv file?

  3. 3

    How to convert tuple to a multi nested dictionary in python?

  4. 4

    Convert nested dictionary into dataframe

  5. 5

    python dictionary and list: how to convert it?

  6. 6

    How to store CSV data in a Nested Dictionary that has a dictionary and a list?

  7. 7

    How to convert a dictionary into a flat list?

  8. 8

    Convert nested class to dictionary

  9. 9

    How convert list of dictionaries to a dictionary of dictionaries in Python

  10. 10

    how to create a nested dictionary of list

  11. 11

    How to convert Dictionary to List with mixing of values and keys?

  12. 12

    How to convert this list into a dictionary

  13. 13

    Convert list of nested dictionary to a flat list of dictionary

  14. 14

    How to convert a list of tuples to a dictionary of dictionaries in Python?

  15. 15

    Flatten nested dictionary - convert list element to string

  16. 16

    Flatten nested dictionary - convert list element to string

  17. 17

    How to create a nested dictionary from a list in Python?

  18. 18

    How to convert multiple list to dictionary

  19. 19

    how to create a nested dictionary of list

  20. 20

    How to write a list with a nested dictionary to a csv file?

  21. 21

    How to user LINQ to convert list to dictionary?

  22. 22

    How to convert this dictionary into a list of tuples?

  23. 23

    How to extract from this list of tuples and convert into this dictionary?

  24. 24

    python dictionary and list: how to convert it?

  25. 25

    How to convert aspell dictionary to simple list of words?

  26. 26

    How to convert list of list to dictionary in Python?

  27. 27

    How to convert list into nested list in Haskell

  28. 28

    How to convert a nested list to a single list?

  29. 29

    How to convert nested dictionary to dataframe in Python

HotTag

Archive