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

    Join Date
    Jan 2013
    Posts
    13
    Rep Power
    0

    Issue clearing list


    Re:v2.6
    I'm in a loop and want to clear list1 after I append to list2.

    For example:

    list1 = []
    list2 = []
    for i in range(0, 100)):
    list1 .append(var1)
    list1 .append(var2)
    list2 .append(list1 )

    # at this point I want to clear list1 for the next iteration.
    # any of the following clears list1 but also clears list2.
    # I want list2 to remain untouched.
    list1 .pop()
    del list1 [:]

    # my current work around is to do a list1=[] each iteration but
    # that creates another instance of list1 and I don't want that.

    Any ideas?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    516
    Rep Power
    34
    my current work around is to do a list1=[] each iteration but that creates another instance of list1
    and removes/garbage collects the previous instance leaving one only. You can also do
    Code:
    ## don't use "i" ,"l" or "o" as they look like numbers
    for ctr in range(0, 100)):
       list2.append([var1, var2])
    ##
    ##   or list comprehension
    list2 = [[var1, var2] for ctr in range(100)]
    Last edited by dwblas; January 17th, 2013 at 05:17 PM.

IMN logo majestic logo threadwatch logo seochat tools logo