1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Rep Power

    Adding and deleting elements within lists at the same time

    How does one add to a list while deleting from a list in one string? For example, here's a rundown of the different commands I understand.

    ## Create a simple list 
    inventory = ["notebook", "pencils", "textbooks"] 
    ## Splice the list to give you "['notebook', 'pencils']" 
    ## Add "calculator" to the list 
    inventory += ["calculator"] 
    ## Reassign an index 
    inventory[0] = "notepad" 
    ## Delete an element 
    del inventory[2] 
    ## delete a slice 
    del inventory[:2] 
    ## Add an element, while deleting a different element at the same time 
    I'm just wondering if it were possible, using either well-placed commas or concatenating, to multitask and reduce the number of lines. Also, I noticed that URLs are not allowed. How about using pastebin?
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Rep Power

    I cannot think of a way to satisfy you.

    del is a statement unto itself.
    As is assignment.
    The extend method returns None .
    As does append.

    A statement like

    A[:2] + [1,2,3] + A[6:] + list('cba')

    has created a new list, not modified A .
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo