Python list(set()) inside a loop is splitting element by letter

In a loop I'm trying to make an list.

sedol_list = []
for d in self.holdings_history.keys():
        for i in np.arange(0,len(self.holdings_history[d]),1) :
            sedol_list = list(set(self.holdings_history[d][i].sedol)) 

This used to work, each sedol was adding to the list.

Today it is not working but rather splitting each "sedol" by letter and adding each letter to the sedol_list.

It then moves to the next "sedol" splits it by letter and then overwrites the sedol_list.

I've made no changes to my packages, running on windows (Visual Studio Code) and linux environments with the same issue...

Prelim test with Spyder looked ok

1 answer

  • answered 2018-08-09 00:16 juliusmh

    Try this:

    sedol_list = set()
    for d in self.holdings_history.keys():
            for i in np.arange(0,len(self.holdings_history[d]),1) :
                sedol_list.add(self.holdings_history[d][i].sedol))
    

    You basically used the set method on a string (which is syntactically right because strings are arrays in python and other languages) so you just get a bunch of non redundant characters from a string. Hopefully this fixed your problem!