Not to print None for keys without values in a dictionary
Question:
A dictionary was created with keys. Later some of the keys got values updated.
When printing the keys in a For loop, the keys without values, prints None.
ma_dict = dict.fromkeys(["David","Mike","Kate","Lilly"])
ma_dict.update({"David" : "Front", "Mike" : "Middle"})
for each in ["David","Mike","Kate","Lilly"]:
print (each, ma_dict[each])
It prints:
David Front
Mike Middle
Kate None
Lilly None
What is the way not to print the None?
# outputs wanted
David Front
Mike Middle
Kate
Lilly
Not to use an If statement, such as:
ma_dict = dict.fromkeys(["David","Mike","Kate","Lilly"])
ma_dict.update({"David" : "Front", "Mike" : "Middle"})
for each in ["David","Mike","Kate","Lilly"]:
if ma_dict[each] != None:
print (each, ma_dict[each])
else:
print (each)
Answers:
I think using if
is appriopate here, but if you must Not to use an If statement AT ANY PRICE then do
ma_dict = dict.fromkeys(["David","Mike","Kate","Lilly"])
ma_dict.update({"David" : "Front", "Mike" : "Middle"})
for each in ["David","Mike","Kate","Lilly"]:
print(each, [ma_dict[each],""][ma_dict[each] is None])
output
David Front
Mike Middle
Kate
Lilly
If it’s just for printing, you might consider using a dictionary comprehension to replace None
with empty strings:
d = {k: ma_dict[k] if ma_dict[k] is not None else "" for k in ma_dict}
for k in d:
print(k, d[k])
David Front
Mike Middle
Kate
Lilly
Inspired by @masasa:
from collections import defaultdict
ma_dict = defaultdict(str)
ma_dict.update({"David" : "Front", "Mike" : "Middle"})
for each in ["David","Mike","Kate","Lilly"]:
print (each, ma_dict[each])
It prints:
David Front
Mike Middle
Kate
Lilly
Since you don’t want to use an if
condition and As @masasa has suggested, You could use a defaultdict
.
from collections import defaultdict
ma_dict = defaultdict.fromkeys(["David","Mike","Kate","Lilly"], '')
ma_dict.update({"David" : "Front", "Mike" : "Middle"})
for each in ["David","Mike","Kate","Lilly"]:
print (each, ma_dict.get(each))
David Front
Mike Middle
Kate
Lilly
A dictionary was created with keys. Later some of the keys got values updated.
When printing the keys in a For loop, the keys without values, prints None.
ma_dict = dict.fromkeys(["David","Mike","Kate","Lilly"])
ma_dict.update({"David" : "Front", "Mike" : "Middle"})
for each in ["David","Mike","Kate","Lilly"]:
print (each, ma_dict[each])
It prints:
David Front
Mike Middle
Kate None
Lilly None
What is the way not to print the None?
# outputs wanted
David Front
Mike Middle
Kate
Lilly
Not to use an If statement, such as:
ma_dict = dict.fromkeys(["David","Mike","Kate","Lilly"])
ma_dict.update({"David" : "Front", "Mike" : "Middle"})
for each in ["David","Mike","Kate","Lilly"]:
if ma_dict[each] != None:
print (each, ma_dict[each])
else:
print (each)
I think using if
is appriopate here, but if you must Not to use an If statement AT ANY PRICE then do
ma_dict = dict.fromkeys(["David","Mike","Kate","Lilly"])
ma_dict.update({"David" : "Front", "Mike" : "Middle"})
for each in ["David","Mike","Kate","Lilly"]:
print(each, [ma_dict[each],""][ma_dict[each] is None])
output
David Front
Mike Middle
Kate
Lilly
If it’s just for printing, you might consider using a dictionary comprehension to replace None
with empty strings:
d = {k: ma_dict[k] if ma_dict[k] is not None else "" for k in ma_dict}
for k in d:
print(k, d[k])
David Front
Mike Middle
Kate
Lilly
Inspired by @masasa:
from collections import defaultdict
ma_dict = defaultdict(str)
ma_dict.update({"David" : "Front", "Mike" : "Middle"})
for each in ["David","Mike","Kate","Lilly"]:
print (each, ma_dict[each])
It prints:
David Front
Mike Middle
Kate
Lilly
Since you don’t want to use an if
condition and As @masasa has suggested, You could use a defaultdict
.
from collections import defaultdict
ma_dict = defaultdict.fromkeys(["David","Mike","Kate","Lilly"], '')
ma_dict.update({"David" : "Front", "Mike" : "Middle"})
for each in ["David","Mike","Kate","Lilly"]:
print (each, ma_dict.get(each))
David Front
Mike Middle
Kate
Lilly