简体   繁体   中英

How to add two values of the same key if there are more than two values in that key itself?

so i have a dictionary:

a={'price':[1,2,3],'code':['abc','abc','def']}

I need to add the first two values of the key 'price' and store it in a variable. How can i do that?

basically what i want to do is add 1 and 2 not 3 in the key 'price'.

The following should give what you want:

new_var=a['price'][0]+a['price'][0]


 or

new_var=sum(a['price'][:2])

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