9 changing your mind


#1

Your code here!

del zoo_animals['Sloth']
del zoo_animals['Bengal Tiger']
zoo_animals ['Rockhopper Penguin'] = 'Crazy Forest'

I had a question about syntax. To make the code a little simpler I was curious what the correct way would be to include to list items into a "del" command? I tried

del zoo_animals['Sloth', 'Bengal Tiger']
and
del zoo_animals['Sloth' 'Bengal Tiger']

Neither of these work, wondering if anyone knew how to put two items in one?

Thanks


#2

@moremove,
If you have such a question....
google search
== the Book ==
del site:python.org

== discussions /opinions ==
python del array element site:stackoverflow.com
for instance
= http://stackoverflow.com/questions/627435/how-to-remove-an-element-from-a-list-by-index-in-python
or
= http://stackoverflow.com/questions/497426/deleting-multiple-elements-from-a-list
[with quote]

def multi_delete(list_, *args):
    indexes = sorted(list(args), reverse=True)
    for index in indexes:
        del list_[index]
    return list_

[quote]

== changed and tested ==

indexe=["l","a","b","c","k","h"]
def multi_delete(list_, *args):
    indexes = sorted(args, reverse=True)
    print sorted(args,reverse=True)
    print type(indexes)
    print list_
    for index in indexes:
        print list_.index(index)
        print index
        del list_[list_.index(index)]
    return list_
    
print multi_delete(["a","b","c","k","h"],"b","k","a")

[output]

['k', 'b', 'a']
<type 'list'>
['a', 'b', 'c', 'k', 'h']
3
k
1
b
0
a
['c', 'h']

print multi_delete(indexe,"b","k","a")

[output]

['k', 'b', 'a']
<type 'list'>
['l', 'a', 'b', 'c', 'k', 'h']
4
k
2
b
1
a
['l', 'c', 'h']

#3

I done this from line 12-15.

Your code here!

del zoo_animals['Sloth']
del zoo_animals['Bengal Tiger']
zoo_animals['Rockhopper Penguin'] = 'africa zoo Exhibit'