<div dir="ltr">On Sun, Jul 14, 2013 at 1:42 PM, David Mertz <span dir="ltr"><<a href="mailto:mertz@gnosis.cx" target="_blank">mertz@gnosis.cx</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">Another approach in one of the links Sergey gave is nice too, and shorter and more elegant than any of his alternatives:<br><div class="gmail_extra"><div class="gmail_quote"><div><br></div>
<div> flat = []<br></div><div> map(flat.extend, list_of_lists)<br><br></div><div>Using map() for a side effect is slightly wrong, but this is short, readable, and obvious in purpose.<br></div></div></div></div></blockquote>
<div><br></div><div>Oh yeah. That's a Python 2.x thing. Now map() doesn't actually have side-effects. So you'd need to do:<br><br></div><div> flat = []<br></div><div> set(map(flat.extend, list_of_lists))<br>
<br></div><div>That starts to border on ugly. One might also try:<br><br></div><div> flat = []<br></div><div> [flat.extend(l) for l in list_of_lists]<br><br></div><div>But I'm not thrilled by how that reads either. Using the chain() versions is just nicer. Moreover, if you insist on concrete collections out of it, you can take your pick (unlike sum().. although you can obviously wrap that answer in a constructor too):<br>
<br></div><div> flat_tup = tuple(chain(*iter_of_lists))<br></div><div> flat_set = set(chain(list_of_lists))<br></div><div> </div></div><br>-- <br>Keeping medicines from the bloodstreams of the sick; food <br>from the bellies of the hungry; books from the hands of the <br>
uneducated; technology from the underdeveloped; and putting <br>advocates of freedom in prisons. Intellectual property is<br>to the 21st century what the slave trade was to the 16th.<br>
</div></div>