In general, I agree with you that code should be readable.  On the other hand, we don&#39;t necessarily want to be writing for the lowest common denominator, i.e., the newcomer. <br><br>There&#39;s room for abuse in any language, but I don&#39;t consider any particular construct (or language) &quot;evil&quot; per se.   Otherwise, you can extend that appellation to anything beyond pure machine code since, for the &quot;newcomer&quot; symbolic instruction sets and higher-level, algebraic languages and even macro-assemblers reduce what&#39;s really going on to &quot;one-liners&quot;.  You might see the in-line &quot;for&quot; construct as coming from perl, but in fact it&#39;s been around MUCH longer than that, going back to  list-directed I/O in FORTRAN IV.<br>

<br>What I think is good, here, is that we&#39;ve managed to show the questioner that there are two ways to solve the problem, discuss the relative merits of both, concluding that  yours is, indeed, the more computationally efficient approach.  The nice by-product is that it provokes thought as to what may be lacking in the language that would otherwise allow the one-liner approach to be used.<br>

<br>Cheers,<br>Nick<br><br><div class="gmail_quote">On Fri, Apr 30, 2010 at 6:47 PM, Kevin Littlejohn <span dir="ltr">&lt;<a href="mailto:darius@obsidian.com.au">darius@obsidian.com.au</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

<br><br><div class="gmail_quote"><div class="im">On 30 April 2010 18:35, N6151H <span dir="ltr">&lt;<a href="mailto:n6151h@gmail.com" target="_blank">n6151h@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">



Sorry.  Should have included l1, which is:  [[&#39;a&#39;, 1], [&#39;b&#39;, 2], [&#39;a&#39;, 3], [&#39;b&#39;, 4]]<br>Same as what the original poster wanted (substituting strings for the A and B objects, WoLOG.<br><br>



Your example presumes test to be an already-populated dict, I think, which is, to my understanding, where the original poster wanted </blockquote><div><br></div></div><div>Sorry, you&#39;re right, that &quot;.items()&quot; shouldn&#39;t be there.  I added it in a fit of stupidity.</div>



<div><br></div><div>I really dislike the option you gave because one line expressions are evil.  They&#39;re fine for the initial write, they&#39;re hell on the new guy in the company trying to upkeep your code, and they&#39;re horrible to have to debug later on.  Having maintained the same body of python code for over 10 years (yes, it was initially written under python 1.x), I would rate readability a higher requirement than brevity.  Otherwise, you&#39;d be better off in perl, right? :)</div>



<div><br></div><div>I don&#39;t think offering one-liners to newcomers is wise - at least, not without also giving them a longhand version.  I particularly think if you&#39;re going to do it as a one-liner, you want to make sure you&#39;re gaining something for the sacrificed readability - in the case provided, your one liner was also less efficient than doing it longhand.  That seems to often be the case in Python - write it out naturally, and it&#39;s likely to be efficient anyway.</div>



<div><br></div><div>But, you&#39;re right, the .items() was a typo ;)</div><div><br></div><div>KJL</div><div><div></div><div class="h5"><div> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

to end up.  He&#39;s starting with a list.  So, you need to build a dict as you iterate through that list, preferrably just once.  The method I suggested does indeed require multiple passes.  The key problem seems to be how to set the initial value for each key to an empty list, which can then be appended.   You can do it in one pass like this:<br>





<br>    d = {}<br>    for k,v in l1:<br>        d.setdefault(k, []).append(v)<br><br> which is probably what you meant to write, rather than test.items(), I suspect.<br><br>But, it would still be nice to get this down to a one-line expression, wouldn&#39;t it?<br>





<br>Would be nice if you had a form of the reduce built-in function that worked with something other than scalars.  Then you could write:<br> <br>    e = {}<br>    e = reduce (lambda k,v: e.setdefault(k,[]).append(v), l1, e)<br>





<br><br>How about<div><div></div><div><br><br><div class="gmail_quote">On Fri, Apr 30, 2010 at 6:00 PM, Kevin Littlejohn <span dir="ltr">&lt;<a href="mailto:darius@obsidian.com.au" target="_blank">darius@obsidian.com.au</a>&gt;</span> wrote:<br>



<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

Suspect you can do it more efficiently with more lines:<div><br></div><div>d = {}</div><div>for key, val in test.items():</div><div>    d.setdefault(key, []).append(val)</div><div><br></div><div>Saves multiple runs through the original list (which I presume is l1 in your example).  Also makes for more readable code.  d comes out the same as your example&#39;s return val.</div>







<div><br></div><div>KJL</div><div><div><div></div><div><br><div class="gmail_quote">On 30 April 2010 17:08, N6151H <span dir="ltr">&lt;<a href="mailto:n6151h@gmail.com" target="_blank">n6151h@gmail.com</a>&gt;</span> wrote:<br>





<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

I believe this is what you want:<br><br>   dict([(r[0], [s[1] for s in l1 if s[0] == r[0]]) for r in l1])<br><br>returns <br><br>   {&#39;a&#39;: [1, 3], &#39;b&#39;: [2, 4]}<br><br>Cheers,<br>Nick<br><br><div class="gmail_quote">









On Fri, Apr 30, 2010 at 3:58 AM, Bill Jordan <span dir="ltr">&lt;<a href="mailto:billjordan121@yahoo.com" target="_blank">billjordan121@yahoo.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">









<div><div style="font-family: times new roman,new york,times,serif; font-size: 12pt;"><div>Hey guys,</div>
<div> </div>
<div>I am sorry if this is not the right list to post some questions. I have a simple question please and would appreciate some answers as I am new to Python.</div>
<div> </div>
<div>I have 2 D array: test = [[A,1],[B,2],[A,3][B,4]]</div>
<div>I want to arrang this array in different arrays so each one will have what is attached to. For example I want the output:</div>
<div> </div>
<div>A =[1,3] and B=[2,4]</div>
<div> </div>
<div>Thanks,</div>
<div>Bill</div></div><font color="#888888"><br>







      </font></div><br>_______________________________________________<br>
melbourne-pug mailing list<br>
<a href="mailto:melbourne-pug@python.org" target="_blank">melbourne-pug@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/melbourne-pug" target="_blank">http://mail.python.org/mailman/listinfo/melbourne-pug</a><br>
<br></blockquote></div><br>
<br>_______________________________________________<br>
melbourne-pug mailing list<br>
<a href="mailto:melbourne-pug@python.org" target="_blank">melbourne-pug@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/melbourne-pug" target="_blank">http://mail.python.org/mailman/listinfo/melbourne-pug</a><br>
<br></blockquote></div><br><br clear="all"><br></div></div>-- <br>Kevin Littlejohn<br>Obsidian Consulting Group<br>ph: +613 9355 7844<br>skype: silarsis<br>
</div>
<br>_______________________________________________<br>
melbourne-pug mailing list<br>
<a href="mailto:melbourne-pug@python.org" target="_blank">melbourne-pug@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/melbourne-pug" target="_blank">http://mail.python.org/mailman/listinfo/melbourne-pug</a><br>
<br></blockquote></div><br>
</div></div><br>_______________________________________________<br>
melbourne-pug mailing list<br>
<a href="mailto:melbourne-pug@python.org" target="_blank">melbourne-pug@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/melbourne-pug" target="_blank">http://mail.python.org/mailman/listinfo/melbourne-pug</a><br>
<br></blockquote></div></div></div><div><div></div><div class="h5"><br><br clear="all"><br>-- <br>Kevin Littlejohn<br>Obsidian Consulting Group<br>ph: +613 9355 7844<br>skype: silarsis<br>
</div></div><br>_______________________________________________<br>
melbourne-pug mailing list<br>
<a href="mailto:melbourne-pug@python.org">melbourne-pug@python.org</a><br>
<a href="http://mail.python.org/mailman/listinfo/melbourne-pug" target="_blank">http://mail.python.org/mailman/listinfo/melbourne-pug</a><br>
<br></blockquote></div><br>