This is my code:
a = dict(aa='aaaa', bb='bbbbb', cc='ccccc', ...)
print(a.pop(['cc', ...]))
but this raises an error. What is the best simple way to pop many elements from a python dictionary?
Python Dictionary pop() MethodPython pop() method removes an element from the dictionary. It removes the element which is associated to the specified key. If specified key is present in the dictionary, it remove and return its value.
The pop() method removes the specified item from the dictionary. The value of the removed item is the return value of the pop() method, see example below.
How about the simple:
for e in ['cc', 'dd',...]:
a.pop(e)
Using list comprehension:
a = {'key1':'value1','key2':'value2','key3':'value3'}
print [a.pop(key) for key in ['key1', 'key3']]
If I understand correctly what you want, this should do the trick:
print [a.pop(k) for k in ['cc', ...]]
Be careful, though, because pop
is destructive, i.e. it modifies your dictionary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With