this is my python code
mylist = ['a', 'f', 'z']
old_d = {'a': 'aaa', 'b': 'bbb', 'c': 'ccc', 'f': 'fff', 'g':'ggg', 'z':'zzz'}
new_d = {}
for key in mylist:
new_d[key] = old_d[key]
Can we write the above code using list comprehensions or something similar like
new_d[key] = old_d[key] for key in mylist
In Python 2.7 and above you can use a dict comprehension:
In Python 2.6 and below, you don’t have dict comprehensions must use
dictwith a generator or list comprehension: