Murtuza_07 Murtuza_07 - 9 days ago 5
JSON Question

How to convert dictionary with list values to a table

I have dictionary:

{"A": [1,2,3], "B": [5,7,9], "C":[12,5,6]}


I would like it to be in the following format of a table:

A | 1
A | 2
A | 3
B | 5
B | 7
. | .
. | .
C | 6


Thank you!

Answer
d = {"A": [1,2,3], "B": [5,7,9], "C":[12,5,6]}
# make dictionary to list
l = []
for key, value in d.iteritems():
    temp = (key, value)
    l.append(temp)
# sort list
l = sorted(l, key = lambda x: x[0])
# finally!
for key, lists in l:
    for item in lists:
        print( "{}|{}".format(key, item) )