[Tutor] reducing lists within list to their set of unique values

Treder, Robert Robert.Treder at morganstanley.com
Tue May 21 00:49:08 CEST 2013


Hi python folks,
 
I have a list of lists that looks something like this: 
 
tst = [ [], ['test'], ['t1', 't2'], ['t1', 't1', 't2'] ]
 
I want to change the empty sets to a blank string, i.e., '' and the lists with repeat values to the unique set of values. So I have done the following: 
 
>>> for t in tst:
	if len(t) == 0:
		tst.__setitem__(tst.index(t), '')
	else:
		tst.__setitem__(tst.index(t), set(t))

What I get in return is 

>>> tst
['', set(['test']), set(['t2', 't1']), set(['t2', 't1'])]

The empty list is fine but the other lists seem to be expressions rather than values. What do I need to do to simply get the values back liike the following? 

['', ['test'], ['t2', 't1'], ['t2', 't1']]


Thanks, 
Bob
 



--------------------------------------------------------------------------------

NOTICE: Morgan Stanley is not acting as a municipal advisor and the opinions or views contained herein are not intended to be, and do not constitute, advice within the meaning of Section 975 of the Dodd-Frank Wall Street Reform and Consumer Protection Act. If you have received this communication in error, please destroy all electronic and paper copies and notify the sender immediately. Mistransmission is not intended to waive confidentiality or privilege. Morgan Stanley reserves the right, to the extent permitted under applicable law, to monitor electronic communications. This message is subject to terms available at the following link: http://www.morganstanley.com/disclaimers. If you cannot access these links, please notify us by reply message and we will send the contents to you. By messaging with Morgan Stanley you consent to the foregoing.


More information about the Tutor mailing list