Add list elements to dict values

Viewed 49

I'm learning python and having a hard time with this code. I need to update every dict key value with the elements in the list. The list and dict will always have the same length.

list = [1, 2, 3, 4, 5]
dict = {2433: 0, 2429: 0, 2425: 0, 2423: 0, 2427: 0}

Expected output:

dict = {2433: 1, 2429: 2, 2425: 3, 2423: 4, 2427: 5}
1 Answers

IIUC, you want to create a dictionary from a list of keys (or a dummy dictionary) and a list of values?

lst = [1, 2, 3, 4, 5]
dct = {2433: 0, 2429: 0, 2425: 0, 2423: 0, 2427: 0}

new_dct = dict(zip(dct, lst))

output: {2433: 1, 2429: 2, 2425: 3, 2423: 4, 2427: 5}

NB. do not use list and dict as variable names, those are python builtins

Related