You were close, try the snippet below:
>>> my_dict = {
... "keyA": 1,
... "keyB": 2,
... "keyC": 3
... }
>>> invalid = {"keyA", "keyB"}
>>> def without_keys(d, keys):
... return {x: d[x] for x in d if x not in keys}
>>> without_keys(my_dict, invalid)
{'keyC': 3}
Basically, the if k not in keys will go at the end of the dict comprehension in the above case.
You were close, try the snippet below:
>>> my_dict = {
... "keyA": 1,
... "keyB": 2,
... "keyC": 3
... }
>>> invalid = {"keyA", "keyB"}
>>> def without_keys(d, keys):
... return {x: d[x] for x in d if x not in keys}
>>> without_keys(my_dict, invalid)
{'keyC': 3}
Basically, the if k not in keys will go at the end of the dict comprehension in the above case.
In your dictionary comprehension you should be iterating over your dictionary (not k , not sure what that is either). Example -
return {k:v for k,v in d.items() if k not in keys}
Hi All
The title is a bit of a mess so Ill explain, heres a dictionary
asdf = {
"a": 1,
"b": 2,
"c": 3,
}
but lets say I want the "b" key to be conditionally added based on some other variable foo
If I do this
asdf = {
"a": 1,
"b": 2 if foo else None,
"c": 3,
}
If foo is False, then the output is
{
"a": 1,
"b": None,
"c": 3,
}But I want to exclude the key and value and get this output
{
"a": 1,
"c": 3,
}Is there any way of doing this inline and not having to something like this
asdf = {
"a": 1,
"c": 3,
}
if foo:
asdf["b"] = 2python - How to exclude a specific element in a dictionary? - Stack Overflow
Copy a dictionary, except some keys - Ideas - Discussions on Python.org
python - Exclude specific keys of dict when passing to variable - Stack Overflow
Retain all entries except for one key python - Stack Overflow
You can simply do
name = choice(tuple(set(my_dict.values()) - {"name300000.&**"}))
This is a possible solution:
from random import choice
my_dict = {1: "name1", 2: "name2", 3:"name300000.&**", 4:"name4"}
name = choice(list(my_dict.values()))
while name == "name300000.&**":
name = choice(my_dict.values())
print(name)
Or, another possible solution is:
from random import choice
my_dict = {1: "name1", 2: "name2", 3:"name300000.&**", 4:"name4"}
while True:
name = choice(list(my_dict.values()))
if name != "name300000.&**":
break
print(name)
The point is that you have to emulate a do while loop to correctly solve this problem, and in python, since it is not natively present, you can write in these ways.
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]
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.
Add some prefix for attributes you don't want to save and exclude them while saving.
This approach won't work and so is not going to solve your problem. You should instead extend your framework to allow you to specify which attributes were to be omitted from the save/load.
However, it seems odd that you aren't using one of the built in persistence techniques, e.g. pickle, that already offer such features.
def equal_dicts(d1, d2, ignore_keys):
d1_filtered = {k:v for k,v in d1.items() if k not in ignore_keys}
d2_filtered = {k:v for k,v in d2.items() if k not in ignore_keys}
return d1_filtered == d2_filtered
EDIT: This might be faster and more memory-efficient:
def equal_dicts(d1, d2, ignore_keys):
ignored = set(ignore_keys)
for k1, v1 in d1.iteritems():
if k1 not in ignored and (k1 not in d2 or d2[k1] != v1):
return False
for k2, v2 in d2.iteritems():
if k2 not in ignored and k2 not in d1:
return False
return True
Using dict comprehensions:
>>> {k: v for k,v in d1.items() if k not in ignore_keys} == \
... {k: v for k,v in d2.items() if k not in ignore_keys}
Use .viewitems() instead on Python 2.