I have the following dict:
d = {
"one": False,
"two": True,
"three": False,
"four": True,
"five": False
}
I want to replace each instance of True
by "Hello"
and each instance of False
by Good night
.
In the end, I want the following dict:
d = {
"one": "Good night",
"two": "Hello",
"three": "Good night",
"four": "Hello",
"five": "Good night"
}
Is there a way to do it without a for loop, or without iterating each value?
Edit: I can still use a for, I just want to know if there's a shorter way to do it than a for block. Also I wanted to know if there was a shorthand to "map" booleans to values in a dicitonary.
I can't manage to do it without browsing/looping through each key/value of the dict. Based on other answers, I tried this: d = d.update( (k,"Hello") for k in d )
but this is for replacing all values, and not on a condition.
question from:
https://stackoverflow.com/questions/66049789/replace-all-boolean-values-in-a-dict-by-a-corresponding-value-without-looping-th 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…