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]
Answer from Samus_ on Stack Overflowfor 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.
First of all, Im a beginner, so please explain it simple.
for example we have a dictionary of keys like username and password and gmail and some values, I want to print all the keys and values except password key. so what should I write?
I've tried this but it won't work.
information = {
"Username" : "Guest",
"Password" : "Guest1234",
"Gmail" : "Guest1234@gmail.com",
"Gender" : "Unknown",
"Phone" : 123456789
}
for x,y in information.items():
print(x,y except "Password")
Copy a dictionary, except some keys - Ideas - Discussions on Python.org
python - Return copy of dictionary excluding specified keys - Stack Overflow
python - delete all keys except one in dictionary - Stack Overflow
python - Is it possible to iterate through all of a dictionary's keys except a specific subset? - Stack Overflow
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}
Why don't you just create a new one?
lang = {'en': lang['en']}
Edit: Benchmark between mine and jimifiki's solution:
$ python -m timeit "lang = {'ar':'arabic', 'ur':'urdu','en':'english'}; en_value = lang['en']; lang.clear(); lang['en'] = en_value"
1000000 loops, best of 3: 0.369 usec per loop
$ python -m timeit "lang = {'ar':'arabic', 'ur':'urdu','en':'english'}; lang = {'en': lang['en']}"
1000000 loops, best of 3: 0.319 usec per loop
Edit 2: jimifiki's pointed out in the comments that my solution keeps the original object unchanged.
This is quite fast:
En_Value = lang['en']
lang.clear()
lang['en'] = En_Value
Remember that the keys of a dictionary are unique, so using set operations would be suitable (and are very performant):
dictionary = {i: i for i in range(1, 11, 1)}
for key in set(dictionary) - set([1, 2, 3]):
print(key)
You can also use a set literal instead of an explicit set conversion like this:
for key in set(dictionary) - {1, 2, 3}:
print(key)
And, as pointed out in the comments, dictionary.keys() as you originally had it would behave in the same way as set(dictionary).
A technique to bypass a few iterations from a loop would be to use continue.
dictionary = {1: 1, 2: 2, 3 : 3, 4: 4}
for key in dictionary:
if key in {1, 2, 3}:
continue
print(key)