<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">On Sat, Feb 2, 2019 at 3:23 PM Christopher Barker <<a href="mailto:pythonchb@gmail.com">pythonchb@gmail.com</a>> wrote:</div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><div>performance asside, I use numpy because:<br></div><div><br></div><div>c = np.sqrt(a**2 + b**2)</div><div><br></div><div>is a heck of a lot easer to read, write, and get correct than:</div><div><br></div><div><div><font face="monospace, monospace">c = list(map(math.sqrt, map(lambda x, y: x + y, map(lambda x: x**2, a),</font></div><div><font face="monospace, monospace">                                                map(lambda x: x**2, b)</font></div><div><font face="monospace, monospace">                              )))</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">or:</font></div><div><font face="monospace, monospace"><br></font></div><div><div><font face="monospace, monospace">[math.sqrt(x) for x in (a + b for a, b in zip((x**2 for x in a),</font></div><div><font face="monospace, monospace">                                              (x**2 for x in b)</font></div><div><font face="monospace, monospace">                                              ))]</font></div></div></div></div></div></div></div></div></div></blockquote><div><br></div><div>You can also write</div><div><br></div><div>    c = [math.sqrt(x**2 + y**2) for x, y in zip(a, b)]<br></div><div><br></div><div>or</div><div><br></div><div>    c = list(map(lambda x, y: math.sqrt(x**2 + y**2), a, b))<br></div><div><br></div><div>or, since math.hypot exists,</div><div><br></div><div>    c = list(map(math.hypot, a, b))<br></div><div><br></div><div>In recent Python versions you can write [*map(...)] instead of list(map(...)), which I find more readable.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div class="gmail_quote"><div>a_list_of_strings.strip().lower().title()<br></div><div><br></div><div>is a lot nicer than:</div><div><br></div><div>[s.title() for s in (s.lower() for s in [s.strip(s) for s in a_list_of_strings])]</div><div><br></div><div>or </div><div><br></div><div>list(map(str.title, (map(str.lower, (map(str.strip, a_list_of_strings)))) # untested</div></div></div></div></div></div></div></blockquote><div> </div><div>In this case you can write</div><div><br></div><div>    [s.strip().lower().title() for s in a_list_of_strings]</div><div><br></div><div>-- Ben</div><div><br></div></div></div></div></div></div>