Is there a way to sped-up this code by using reduce() rather than the for loop?
I looked at many similar questions but I did not find an answer.
old_dict = {'a': 1, 'b': 2, 'c': 3}
keys = ['a', 'c', 'd']
new_dict = {}
for key in keys:
new_dict[key] = old_dict.get(key)
print(new_dict)
# prints:
# {'a': 1, 'c': 3, 'd': None}
>Solution :
If you’re not set on using reduce, you can chain a few built in functions together which should be very efficient.
new_dict = dict(zip(keys, map(old_dict.get, keys)))