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

    Join Date
    Dec 2012
    Posts
    14
    Rep Power
    0

    List Inexplicably Changes Size


    Hello,

    This is probably a simple answer for most of you, but I have a loop in which I assign elements of a new list. Following this loop, I print the size of the new list and it is much larger than it was in the loop. Below is the snippet of relevant code:

    Code:
    for i in range(len(dbPartNamesV1)):				
    	dbPartNamesV2.insert(i, dbPartNamesV1[i].replace('-', '_'))
    	dbPartNamesV2.insert(i, dbPartNamesV2[i].lower())
    	print(i)
    #	print(dbPartNamesV2[i])
    
    print(len(dbPartNamesV2))
    The end result is:

    Code:
    .
    .
    .
    2314
    2315
    2316
    2317
    2318
    2319
    4640
    Any help is appreciated!

    -John
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    14
    Rep Power
    0
    As I continue to look at this, could it be that it is inserting two elements into the new list for every one element of the old list? My intent was to overwrite each element so that the new list is the same length as the old list.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    114
    Rep Power
    3
    Emphasis mine:
    Code:
    >>> help(list.insert)
    Help on method_descriptor:
    
    insert(...)
        L.insert(index, object) -- insert object before index

IMN logo majestic logo threadwatch logo seochat tools logo