Moving Places, Subtracting from slices/lists

Discussion in 'Python' started by Mark Sargent, Jun 2, 2005.

  1. Mark Sargent

    Mark Sargent Guest

    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.
     
    Mark Sargent, Jun 2, 2005
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Henry
    Replies:
    6
    Views:
    16,173
    Brian Henry
    Nov 19, 2003
  2. Mark Sargent
    Replies:
    4
    Views:
    326
    Dennis Lee Bieber
    Jun 3, 2005
  3. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    410
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  4. the_skin
    Replies:
    0
    Views:
    282
    the_skin
    Oct 1, 2009
  5. Neatu Ovidiu Gabriel

    Suggestion: PEP for popping slices from lists

    Neatu Ovidiu Gabriel, Aug 8, 2013, in forum: Python
    Replies:
    13
    Views:
    140
    Terry Reedy
    Aug 8, 2013
Loading...

Share This Page