You can access the dict entry and delete it in one step using dict.pop (). Here is creating a dict dd, and then writing it by typing from dd and building zz:
>>> dd = dict(zip("ABC","123")) >>> print dd {'A': '1', 'C': '3', 'B': '2'} >>> zz = dict((k,dd.pop(k)) for k in dd.keys()) >>> print zz {'A': '1', 'C': '3', 'B': '2'} >>> print dd {}
So, after creating zz, all values ββwere removed from dd. If you want to do something selective, add an if condition to the dict:
>>> dd = dict(zip("ABCDEFGHIJKLMNOPQRSTUVWXYZ", range(1,27))) >>> vowels = "AEIOU" >>> zz = dict((k,dd.pop(k)) for k in dd.keys() if k not in vowels) >>> dd {'A': 1, 'E': 5, 'I': 9, 'O': 15, 'U': 21} >>> zz {'C': 3, 'B': 2, 'D': 4, 'G': 7, 'F': 6, 'H': 8, 'K': 11, 'J': 10, 'M': 13, 'L': 12, 'N': 14, 'Q': 17, 'P': 16 , 'S': 19, 'R': 18, 'T': 20, 'W': 23, 'V': 22, 'Y': 25, 'X': 24, 'Z': 26}