Moving Places, Subtracting from slices/lists

Mark Sargent powderkeg at snow.email.ne.jp
Fri Jun 3 02:15:11 EDT 2005


Dennis Lee Bieber wrote:

>On Thu, 02 Jun 2005 16:12:44 +0900, Mark Sargent
><powderkeg at snow.email.ne.jp> declaimed the following in
>comp.lang.python:
>
>  
>
>>How do I get that x to be an integer b4 it is entered into the indice.? 
>>Cheers.
>>
>>    
>>
>	If you really want the index, ask for the index...
>
>  
>
>>>>hc
>>>>        
>>>>
>['Cat', 'roof', 'on', 'a', 'hot', 'tin']
>  
>
>>>>try:
>>>>        
>>>>
>... 	i = hc.index("roof")
>... 	new_hc = hc[:i] + hc[i+1:] + [hc[i]]
>... except ValueError:
>... 	pass
>... 
>  
>
>>>>new_hc
>>>>        
>>>>
>['Cat', 'on', 'a', 'hot', 'tin', 'roof']
>
>  
>
Hi All,

ok, got the following for that.

 >>> hotcat = ['Cat', 'roof', 'on', 'a', 'hot', 'tin']
 >>> i = hotcat.index("roof")
 >>> hotcat = hotcat[:1] + hotcat[i+1:] + [hotcat[1]]
 >>> except ValueError:
  File "<stdin>", line 1
    except ValueError:
         ^
SyntaxError: invalid syntax

Cheers.

P.S. If it's not too much, could you explain this line.?
hotcat[:1] + hotcat[i+1:] + [hotcat[1]]
hotcat[:1] = Cat?
hotcat[i+1:] = all after roof?
what does [hotcat[1]] do.?

Mark Sargent.



More information about the Python-list mailing list