<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Jun 28, 2018 at 8:25 AM, Nicolas Rolin <span dir="ltr"><<a href="mailto:nicolas.rolin@tiime.fr" target="_blank">nicolas.rolin@tiime.fr</a>></span> wrote:<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I use list and dict comprehension a lot, and a problem I often have is to do the equivalent of a group_by operation (to use sql terminology).<br></div></blockquote><div><br></div><div>I don't know from SQL, so "group by" doesn't mean anything to me, but this:</div><div> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">For example if I have a list of tuples (student, school) and I want to have the list of students by school the only option I'm left with is to write<br><br>    student_by_school = defaultdict(list)<br>    for student, school in student_school_list:<br>        student_by_school[school].<wbr>append(student)<br></div></blockquote><div><br></div><div>seems to me that the issue here is that there is not way to have a "defaultdict comprehension"</div><div><br></div><div>I can't think of syntactically clean way to make that possible, though.</div><div> <br></div><div>Could itertools.groupby help here? It seems to work, but boy! it's ugly:</div><div><br></div><div>




<span></span>





<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:15px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures;color:rgb(52,163,39)">In [</span><span class="gmail-s2" style="font-variant-ligatures:no-common-ligatures;color:rgb(46,231,33)"><b>45</b></span><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures;color:rgb(52,163,39)">]: </span><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures">student_school_list</span></p>
<p class="gmail-p2" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:15px;line-height:normal;font-family:Menlo;color:rgb(178,54,34);background-color:rgb(255,255,255)"><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures">Out[</span><span class="gmail-s4" style="font-variant-ligatures:no-common-ligatures;color:rgb(255,59,30)"><b>45</b></span><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures">]:<span class="gmail-Apple-converted-space"> </span></span></p>
<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:15px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures">[('Fred', 'SchoolA'),</span></p>
<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:15px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-Apple-converted-space"> </span>('Bob', 'SchoolB'),</span></p>
<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:15px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-Apple-converted-space"> </span>('Mary', 'SchoolA'),</span></p>
<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:15px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-Apple-converted-space"> </span>('Jane', 'SchoolB'),</span></p>
<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:15px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-Apple-converted-space"> </span>('Nancy', 'SchoolC')]</span></p>
<p class="gmail-p3" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:15px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255);min-height:18px"><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures"></span><br></p>
<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:15px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures;color:rgb(52,163,39)">In [</span><span class="gmail-s2" style="font-variant-ligatures:no-common-ligatures;color:rgb(46,231,33)"><b>46</b></span><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures;color:rgb(52,163,39)">]: </span><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures">{a:[t[</span><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures;color:rgb(52,163,39)">0</span><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures">] </span><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures;color:rgb(52,163,39)"><b>for</b></span><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures"> t </span><span class="gmail-s5" style="font-variant-ligatures:no-common-ligatures;color:rgb(208,59,255)"><b>in</b></span><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures"> b] </span><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures;color:rgb(52,163,39)"><b>for</b></span><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures"> a,b </span><span class="gmail-s5" style="font-variant-ligatures:no-common-ligatures;color:rgb(208,59,255)"><b>in</b></span><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures"> groupby(</span><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures;color:rgb(52,163,39)">sorted</span><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures">(student_school_list, key=</span><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures;color:rgb(52,163,39)"><b>lambda</b></span><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures"> t: t[</span><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures;color:rgb(52,163,39)">1</span><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures">]), key=</span><span class="gmail-s1" style="font-variant-ligatures:no-common-ligatures;color:rgb(52,163,39)"><b>lambda</b></span><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures"> t: t[</span></p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:15px;line-height:normal;font-family:Menlo;color:rgb(52,163,39);background-color:rgb(255,255,255)"><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-Apple-converted-space">    </span>...: 1</span><span class="gmail-s6" style="font-variant-ligatures:no-common-ligatures;color:rgb(0,0,0)">])}</span></p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:15px;line-height:normal;font-family:Menlo;color:rgb(52,163,39);background-color:rgb(255,255,255)"><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-Apple-converted-space">    </span>...:<span class="gmail-Apple-converted-space"> </span></span></p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:15px;line-height:normal;font-family:Menlo;color:rgb(52,163,39);background-color:rgb(255,255,255)"><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-Apple-converted-space">    </span>...:<span class="gmail-Apple-converted-space"> </span></span></p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:15px;line-height:normal;font-family:Menlo;color:rgb(52,163,39);background-color:rgb(255,255,255)"><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-Apple-converted-space">    </span>...:<span class="gmail-Apple-converted-space"> </span></span></p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:15px;line-height:normal;font-family:Menlo;color:rgb(52,163,39);background-color:rgb(255,255,255)"><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-Apple-converted-space">    </span>...:<span class="gmail-Apple-converted-space"> </span></span></p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:15px;line-height:normal;font-family:Menlo;color:rgb(52,163,39);background-color:rgb(255,255,255)"><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-Apple-converted-space">    </span>...:<span class="gmail-Apple-converted-space"> </span></span></p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:15px;line-height:normal;font-family:Menlo;color:rgb(52,163,39);background-color:rgb(255,255,255)"><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-Apple-converted-space">    </span>...:<span class="gmail-Apple-converted-space"> </span></span></p>
<p class="gmail-p4" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:15px;line-height:normal;font-family:Menlo;color:rgb(52,163,39);background-color:rgb(255,255,255)"><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures"><span class="gmail-Apple-converted-space">    </span>...:<span class="gmail-Apple-converted-space"> </span></span></p>
<p class="gmail-p1" style="margin:0px;font-variant-numeric:normal;font-variant-east-asian:normal;font-weight:normal;font-stretch:normal;font-size:15px;line-height:normal;font-family:Menlo;color:rgb(0,0,0);background-color:rgb(255,255,255)"><span class="gmail-s7" style="font-variant-ligatures:no-common-ligatures;color:rgb(178,54,34)">Out[</span><span class="gmail-s4" style="font-variant-ligatures:no-common-ligatures;color:rgb(255,59,30)"><b>46</b></span><span class="gmail-s7" style="font-variant-ligatures:no-common-ligatures;color:rgb(178,54,34)">]: </span><span class="gmail-s3" style="font-variant-ligatures:no-common-ligatures">{'SchoolA': ['Fred', 'Mary'], 'SchoolB': ['Bob', 'Jane'], 'SchoolC': ['Nancy']}</span></p>


<br></div><div><br></div><div>-CHB</div><div><br></div><div><br></div></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><br>Christopher Barker, Ph.D.<br>Oceanographer<br><br>Emergency Response Division<br>NOAA/NOS/OR&R            (206) 526-6959   voice<br>7600 Sand Point Way NE   (206) 526-6329   fax<br>Seattle, WA  98115       (206) 526-6317   main reception<br><br><a href="mailto:Chris.Barker@noaa.gov" target="_blank">Chris.Barker@noaa.gov</a></div>
</div></div>