Retain all entries except for one key python

Question:

I have a python dictionary. Just to give out context, I am trying to write my own simple cross validation unit.

So basically what I want is to get all the values except for the given keys.
And depending on the input, it returns all the values from a dictionary except to those what has been given.

So if the input is 2 and 5 then the output values doesn’t have the values from the keys 2 and 5?

Asked By: frazman

||

Answers:

for key, value in your_dict.items():
    if key not in your_blacklisted_set:
        print value

the beauty is that this pseudocode example is valid python code.

it can also be expressed as a list comprehension:

resultset = [value for key, value in your_dict.items() if key not in your_blacklisted_set]
Answered By: Samus_

How about something along the following lines:

In [7]: d = dict((i,i+100) for i in xrange(10))

In [8]: d
Out[8]: 
{0: 100,
 1: 101,
 2: 102,
 3: 103,
 4: 104,
 5: 105,
 6: 106,
 7: 107,
 8: 108,
 9: 109}

In [9]: exc = set((2, 5))

In [10]: for k, v in d.items():
   ....:     if k not in exc:
   ....:         print v
   ....:         
   ....:         
100
101
103
104
106
107
108
109
Answered By: NPE

Also, as a list comprehension using sets:

d = dict(zip(range(9),"abcdefghi"))
blacklisted = [2,5]
outputs = [d[k] for k in set(d.keys())-set(blacklisted)]
Answered By: Nate

Given a dictionary say

d = {
     2: 2, 5: 16, 6: 5,
     7: 6, 11: 17, 12: 9,
     15: 18, 16: 1, 18: 16,
     19: 17, 20: 10
     }

then the simple comprehension example would attain what you possibly desire

[v for k,v in d.iteritems() if k not in (2,5)]

This example lists all values not with keys {2,5}

for example the O/P of the above comprehension is

[5, 6, 1, 17, 9, 18, 1, 16, 17, 10]
Answered By: Abhijit

Just for fun with sets

keys = set(d.keys())
excludes = set([...])

for key in keys.difference(excludes):
    print d[key]
Answered By: joel3000
keys = ['a', 'b']
a_dict = {'a':1, 'b':2, 'c':3, 'd':4}
[a_dict.pop(key) for key in keys]

After popping out the keys to be discarded, a_dict will retain the ones you’re after.

Answered By: Darren McAffee

If your goal is to return a new dictionary, with all key/values except one or a few, use the following:

exclude_keys = ['exclude', 'exclude2']
new_d = {k: d[k] for k in set(list(d.keys())) - set(exclude_keys)}

where 'exclude' can be replaced by (a list of) key(s) which should be excluded.

Answered By: Dendrobates

using exception handling

facebook_posts = [
    {'Likes': 21, 'Comments': 2}, 
    {'Likes': 13, 'Comments': 2, 'Shares': 1}, 
    {'Likes': 33, 'Comments': 8, 'Shares': 3}, 
    {'Comments': 4, 'Shares': 2}, 
    {'Comments': 1, 'Shares': 1}, 
    {'Likes': 19, 'Comments': 3}
]

total_likes = 0

for post in facebook_posts:
    try:
        total_likes = total_likes + post['Likes']
    except KeyError:
        pass
print(total_likes)

you can also use:

except KeyError: 'My Key'

but probably only good for once off use

Answered By: robot

Be careful of using a shallow copy if you intend to modify the resulting dict.

import copy

def copy_dict_except_keys_shallow(d:dict, exclude_keys):
  return {k: d[k] for k in set(list(d.keys())) - set(exclude_keys)}

def copy_dict_except_keys_deep(d:dict, exclude_keys):
  return {k: copy.deepcopy(d[k]) for k in set(list(d.keys())) - set(exclude_keys)}

original = {'a': 1, 'b': [1, 2, 3]}
deep = copy_dict_except_keys_deep(original, ['a'])
deep['b'].append(4)

print("Modifying the deep copy preserves the original")
print(f"original: {original}")
print(f"deep copy: {deep}")
print()

shallow = copy_dict_except_keys_shallow(original, 'a')
shallow['b'].append(4)

print("Modifying the shallow copy changes the original")
print(f"original: {original}")
print(f"shallow copy: {shallow}")
Modifying the deep copy preserves the original
original: {'a': 1, 'b': [1, 2, 3]}
deep copy: {'b': [1, 2, 3, 4]}

Modifying the shallow copy changes the original
original: {'a': 1, 'b': [1, 2, 3, 4]}
shallow copy: {'b': [1, 2, 3, 4]}
Answered By: RyanHennig
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.