I'm iterating over the list and supposedly add element to dictionary that doesn't occur in dictionary as value, therefore I would expect output:
{'key': 1, 'key2': 3, 'key3': 2, 'key4':4, 'key5':5}
Code:
di = {'key':1, 'key2':4}
li=[1,1,1,2,4,3,5]
b = sum(1 for key in di if key.startswith('key')) # check how many keys starts with 'key'
for i in li: #if element of list is not in dictionary key values
if i not in di.values(): #add it as value to 'key+b+1'
di[f'key{b+1}']= i
But the ouput I'm getting:
{'key': 1, 'key2': 4, 'key3': 5}
So as I see despite I'm telling Python to check elements in dict.values he's checking also keys or items.
question from:
https://stackoverflow.com/questions/65849697/doesnt-add-all-elements-that-are-not-in-dictionary-values 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…