Moving Places, Subtracting from slices/lists

Mark Sargent powderkeg at snow.email.ne.jp
Thu Jun 2 02:53:02 EDT 2005


Hi All,

playing around with the tut now. How can I get this code to remove the 
original instance of 'roof'.?
 >>> hotcat = ['Cat', 'roof', 'on', 'a', 'hot', 'tin']
 >>> for x in hotcat[:]:
...     if x == 'roof': hotcat.insert(6,x)
...
 >>> hotcat
['Cat', 'roof', 'on', 'a', 'hot', 'tin', 'roof']

Perhaps a replace or something after the 2nd line of the for function.?
 >>> hotcat = ['Cat', 'roof', 'on', 'a', 'hot', 'tin']
 >>> for x in hotcat[:]:
...     if x == 'roof': hotcat.insert(6,x)
...     hotcat[x:len(x)] = []
...
Traceback (most recent call last):
  File "<stdin>", line 3, in ?
TypeError: slice indices must be integers

 I feel I'm close to it.

Cheers.

Mark Sargent.




More information about the Python-list mailing list