0
votes

I have multiple dictionaries:

dict1 = {"Hello": [1, 2, 3], "World": [1,2]} 
dict2 = {"Hello": [1], "World":[2]}
dict3 =  {"Test": [1]}

I'm trying to print these dictionaries horizontally based on the key and values from each dictionary. It will print a header (Word, Dict1, Dict2, Dict3 ... etc) then the subsequent word and value from each dictionary horizontally in the correct location.

Example output:

Word, Dict1, Dict2, Dict3
Hello, 1:2:3, 1, 
World, 1:2, 2, 
Test, , , 1

Currently, I'm working with (I can fix formatting later, right now focused on just printing the correct values in the correct location):

my_dicts = [dict1, dict2, dict3]
header = "Word, " + "dict1"+", dict2"+", dict3"
all_possible_keys = set(reduce(lambda x,y: x + y.keys(), my_dicts, []))

print(header)
for k in all_possible_keys:
    print_str = "{},".format(k)
    for d in my_dicts:
        print_str += "{},".format(d.get(k, None))
    print_str += "\n"
    print(print_str)

I'm currently getting the following error on my all_possible_keys assignment:

TypeError: can only concatenate list (not "dict_keys") to list

Any thoughts?

1
change to list(y.keys())?Tomerikoo

1 Answers

1
votes

y.keys() does not return a list. Use list(y.keys()).