Answers for "how do i add new items to a dictionary within a for loop python"

0

how do i add new items to a dictionary within a for loop python

'''
There are 5 items with list as a key. To add a new item 'F' to the dictionary
you would want to use list(d.items()) in for loop
'''

d = {'A': [1, 5, 6, 7], 'B':[4, 5, 6, 7, 9], 'C':[12 ,4, 6, 3, 6], 'D':[23, 5, 7, 23, 12], 'E':[22, 2, 1, 4, 6]}
i = 1 #just for conditional
asc_ltr = 70 #asci value for letter 'F'

for k, v in list(d.items()):
    if i == 3:
        d['A'].append(50) #just checking the extra condition
    else: 
    	#to create a new key and value 
        d.update({chr(asc_ltr): [np.mean(v), 20, 30, 40]})
    i += 1
Posted by: Guest on September-07-2021

Code answers related to "how do i add new items to a dictionary within a for loop python"

Python Answers by Framework

Browse Popular Code Answers by Language