(Python 3) If I have a list of keys and 3 lists of values, how can I append these values into an existing key, value pair in a dictionary? Say I have:
mykeys = ["John", "Sarah", "Lexi, "Cass"]
values1 = [3, 5, 2, 6]
values2 = [17, 18, 12, 21]
values3 = [4, 7, 3, 0]
How can I make it so my dictionary contains all three values in each of the keys. i.e.:
{"John" : [3, 17, 4], "Sarah": [5, 18, 7], "Lexi": [2, 12, 3], "Cass": [6, 21, 0]}
I know how I can append each of the values by hand, but I'm wondering if there's some kind of function I can write to do this for me.
def prepare_dictionary(keys, values1, values2, values3): d = {} for i in range(len(keys)): d[keys[i]] = [values1[i], values2[i], values3[i]] return d mykeys = ["John", "Sarah", "Lexi", "Cass"] values1 = [3, 5, 2, 6] values2 = [17, 18, 12, 21] values3 = [4, 7, 3, 0] print(prepare_dictionary(mykeys, values1, values2, values3))
Get Answers For Free
Most questions answered within 1 hours.