Thanks first, I could understand the second approach easily. The first approach is a bit puzzling. Why are seen=set() and seen.add(x) still necessary there if we can use unique.append(x) alone? Thanks for your enlightenment.<br>
<br><div class="gmail_quote">On Sun, Sep 9, 2012 at 1:59 PM, Donald Stufft <span dir="ltr"><<a href="mailto:donald.stufft@gmail.com" target="_blank">donald.stufft@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>
seen = set()
</div><div>uniqued = []</div><div>for x in original:</div><div> if not x in seen:</div><div> seen.add(x)</div><div> uniqued.append(x)</div><div><br></div><div>or</div><div><br></div><div>
uniqued = []</div><div>for x in oriignal:</div><div> if not x in uniqued:</div><div> uniqued.append(x)</div><div><br></div><div>The difference between is option #1 is more efficient speed wise, but uses more memory (extraneous set hanging around), whereas the second is slower (``in`` is slower in lists than in sets) but uses less memory.</div>
<div class="HOEnZb"><div class="h5">
<div></div>
<p style="color:#a0a0a8">On Sunday, September 9, 2012 at 1:56 AM, John H. Li wrote:</p>
<blockquote type="cite" style="border-left-style:solid;border-width:1px;margin-left:0px;padding-left:10px">
<span><div><div>Many thanks. If I want keep the order, how can I deal with it?<div>or we can list(set([1, 1, 2, 3, 4])) = [1,2,3,4]<br><div><br><br><div>On Sun, Sep 9, 2012 at 1:47 PM, Donald Stufft <span dir="ltr"><<a href="mailto:donald.stufft@gmail.com" target="_blank">donald.stufft@gmail.com</a>></span> wrote:<br>
<blockquote type="cite"><div>
<div>
If you don't need to retain order you can just use a set, </div><div><br></div><div>set([1, 1, 2, 3, 4]) = set([1, 2, 3, 4])</div><div><br></div><div>But set's don't retain order.</div>
<p style="color:#a0a0a8">On Sunday, September 9, 2012 at 1:43 AM, Token Type wrote:</p><blockquote type="cite"><div>
<span><div><div><div>Is there a unique method in python to unique a list? thanks</div><span><font color="#888888"><div>-- </div><div><a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a></div>
</font></span></div></div></span>
</div></blockquote><div>
<br>
</div>
</div></blockquote></div><br></div></div>
</div></div></span>
</blockquote>
<div>
<br>
</div>
</div></div></blockquote></div><br>