简体   繁体   中英

Add list elements to dict values

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}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM