Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!feeder.news-service.com!feeder2.cambriumusenet.nl!feed.tweaknews.nl!209.197.12.246.MISMATCH!nx02.iad01.newshosting.com!newshosting.com!69.16.185.16.MISMATCH!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!spln!extra.newsguy.com!newsp.newsguy.com!not-for-mail From: Chris Torek Newsgroups: comp.lang.python Subject: Re: Converting a set into list Date: 15 May 2011 02:11:11 GMT Organization: None of the Above Lines: 38 Message-ID: References: <87iptdid68.fsf@benfinney.id.au> <871v00j2bh.fsf@benfinney.id.au> NNTP-Posting-Host: p62f894a1837df0b59a8d093b04c4ff126923af1c75cacf40.newsdawg.com X-Newsreader: trn 4.0-test76 (Apr 2, 2001) Originator: torek@elf.torek.net (Chris Torek) Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5403 In article <871v00j2bh.fsf@benfinney.id.au> Ben Finney wrote: >As pointed out: you already know how to create a set from an object; >creating a list from an object is very similar: > > list(set(aa)) > >But why are you doing that? What are you trying to achieve? I have no idea why someone *else* is doing that, but I have used this very expression to unique-ize a list: >>> x = [3, 1, 4, 1, 5, 9, 2, 6] >>> x [3, 1, 4, 1, 5, 9, 2, 6] >>> list(set(x)) [1, 2, 3, 4, 5, 6, 9] >>> Of course, this trick only works if all the list elements are hashable. This might not be the best example since the result is sorted "by accident", while other list(set(...)) results are not. Add sorted() or .sort() if needed: >>> x = ['three', 'one', 'four', 'one', 'five'] >>> x ['three', 'one', 'four', 'one', 'five'] >>> list(set(x)) ['four', 'five', 'three', 'one'] >>> sorted(list(set(x))) ['five', 'four', 'one', 'three'] >>> -- In-Real-Life: Chris Torek, Wind River Systems Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603 email: gmail (figure it out) http://web.torek.net/torek/index.html