How to perform an operation to multiple values in a list/Dictionary?


#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>

del zoo_animals['Unicorn’and 'Sloth’and ‘Bengal Tiger’]

<In what way does your code behave incorrectly? Include ALL error messages.>
the code executes by deleting only the Bengal Tiger

<What do you expect to happen instead?>
To simultaneously delete all the strings

```python

zoo_animals = { 'Unicorn' : 'Cotton Candy House',
'Sloth' : 'Rainforest Exhibit',
'Bengal Tiger' : 'Jungle House',
'Atlantic Puffin' : 'Arctic Exhibit',
'Rockhopper Penguin' : 'Arctic Exhibit'}


del zoo_animals['Unicorn'and 'Sloth'and 'Bengal Tiger']. 
print zoo_animals
<do not remove the three backticks above>

#2

if we look in the docs:

https://docs.python.org/2/tutorial/datastructures.html

there is a section which shows how we can remove multiply entries from a list using del statement:

>>> a = [-1, 1, 66.25, 333, 333, 1234.5]
>>> del a[0]
>>> a
[1, 66.25, 333, 333, 1234.5]
>>> del a[2:4]
>>> a
[1, 66.25, 1234.5]
>>> del a[:]
>>> a
[]

but then looking at dictionaries example:

>>> tel = {'jack': 4098, 'sape': 4139}
>>> tel['guido'] = 4127
>>> tel
{'sape': 4139, 'guido': 4127, 'jack': 4098}
>>> tel['jack']
4098
>>> del tel['sape']
>>> tel['irv'] = 4127
>>> tel
{'guido': 4127, 'irv': 4127, 'jack': 4098}
>>> tel.keys()
['guido', 'irv', 'jack']
>>> 'guido' in tel
True

we only see how we can remove a single item. So the only possibility seems to be to simply tell python that we want to delete multiply keys:

del zoo_animals['Unicorn'], zoo_animals['Sloth']

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.