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

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    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.

    Code:
    ## Create a simple list 
    
    inventory = ["notebook", "pencils", "textbooks"] 
    
    ## Splice the list to give you "['notebook', 'pencils']" 
    
    inventory[0:2] 
    
    ## 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 Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,850
    Rep Power
    481

    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