2

I'm trying to iterate through a spreadsheet and make a set of all the columns in there while adding the values to their respective set.

storage = [ set() ]*35 #there's 35 columns in the excel sheet
for line in in_file: #iterate through all the lines in the file
    t = line.split('\t') #split the line by all the tabs
    for i in range(0, len(t)): #iterate through all the tabs of the line
        if t[i]: #if the entry isn't empty
            storage[i].add(t[i]) #add the ith entry of the to the ith set

if i do this for storage[0].add(t[0]) it works kind of but it adds to ALL the sets in the storage list...why is it doing that? I'm specifying which set I want to add it in. I didn't post what the print out looks like for the sets b/c it's so big but basically every set is the same and has all the entries from the tabs

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
O.rka
  • 29,847
  • 68
  • 194
  • 309
  • possible duplicate of [How do I pass a variable by reference?](http://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference) – TigerhawkT3 Jul 15 '15 at 20:13

2 Answers2

8
storage = [set()] * 35

This creates a list with the same set listed 35 times. To create a list with 35 different sets, use:

storage = [set() for i in range(35)]

This second form ensures set() is called multiple times. The first form only calls it once and then duplicates that single object reference over and over.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0
storage = [ set() ]*35

>>>[id(i) for i in storage]
 [3055749916L,
 3055749916L,
 3055749916L,
 .....
 3055749916L]

You can see all are referencing to same object.So try

storage = [set() for i in range(35)]
>>>[id(i) for i in storage]
[3054483692L,
 3054483804L,
 .....
 3054483916L,
 3054484028L]
itzMEonTV
  • 19,851
  • 4
  • 39
  • 49