D = {0: 'A', 1: 'B', 2: 'C', 3: 'D', 4: 'E', 5: 'F'}
removeKeys = [0, 2, 5]
X = {k: D[k] for k in D.keys() - removeKeys}
The variable
removeKeys
is a simple
List
. The result of the call to
D.keys()
is
another List
actually a view of the dictionary consisting of just the key values:
keys = D.keys()
D.keys - removeKeys
So the dictionary comprehension iterates the list { 1, 3, 4 } and creates a new dictionary using those values as the keys.