<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Pythoncomprehensions on Janusworx</title><link>https://janusworx.com/tags/pythoncomprehensions/</link><description>Recent content in Pythoncomprehensions on Janusworx</description><generator>Hugo -- gohugo.io</generator><language>en</language><managingEditor>feedback@janusworx.com (Mario Jason Braganza)</managingEditor><webMaster>feedback@janusworx.com (Mario Jason Braganza)</webMaster><copyright>© 2026, Mario Jason Braganza</copyright><lastBuildDate>Sun, 26 Jul 2020 19:00:00 +0530</lastBuildDate><atom:link href="https://janusworx.com/tags/pythoncomprehensions/index.xml" rel="self" type="application/rss+xml"/><item><title>A Hundred Days of Code, Day 018 - Python Comprehensions, Done!</title><link>https://janusworx.com/work/a-hundred-days-of-code-day-018-python-comprehensions-done/</link><pubDate>Sun, 26 Jul 2020 19:00:00 +0530</pubDate><author>feedback@janusworx.com (Mario Jason Braganza)</author><guid>https://janusworx.com/work/a-hundred-days-of-code-day-018-python-comprehensions-done/</guid><description>&lt;p&gt;Continuing my comprehensions journey.&lt;br&gt;
Hopefully it makes more sense this time around&lt;/p&gt;
&lt;!-- TEASER_END --&gt; 

&lt;h2 class="relative group"&gt;Notes
 &lt;div id="notes" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#notes" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h2&gt;
&lt;p&gt;** Set Comprehensions **&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If I want a set of uniques from a group, I could use a set.
&lt;ul&gt;
&lt;li&gt;Or like Reuven likes to joke, a set is a dictionary with no values; an immoral dictionary 😂&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Create it with a &lt;code&gt;somesetcomp = set('somecondition' for a something in somethingbig)&lt;/code&gt; or in a simpler manner, &lt;code&gt;somesetcomp = {'somecondition' for something in somethingbig}&lt;/code&gt;
&lt;ul&gt;
&lt;li&gt;Protip, better to start with a list comprehension, be done with figuring out stuff and then just switch the brackets at the end. Fewer issues that way.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;** Dictionary Comprehensions **&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Create it like so &lt;code&gt;somedictcomp = {somekey:somevalue for something in somethingbig}&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;It creates only one dictionary, not a set&lt;/li&gt;
&lt;li&gt;It &lt;em&gt;has&lt;/em&gt; to have a key and a value variable, seperated by a colon. If it is just a single variable, I’ll end up with a set comprehension.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;** Nested Comprehensions **&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Running a comprehension on the elements of a parent comphrehension is a nested comprehension.
&lt;ul&gt;
&lt;li&gt;For e.g. if I had a list of lists and I needed to pull out each element from each list I would
&lt;ul&gt;
&lt;li&gt;create a comprehension that would get each list from my giant list of lists&lt;/li&gt;
&lt;li&gt;and then run a comprehension on those lists to get each element out.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;big_list = [[10, 20], [30, 40], [50, 60], [70, 80]]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;elemental_list = [each_element for each_list in big_list for each_element in each_list]&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Pretty handy when needed. I can go as many levels deep as needed.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;** Why Comprehensions? **&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A comprehension is needed or rather the better tool for the job, when I have one sequence and I want to turn it into another sequence. Creating new things from old things&lt;/li&gt;
&lt;li&gt;If I am interested in the side effects, the actions, umm, like printing to the screen for example, that is when I’d use &lt;code&gt;for&lt;/code&gt; loops.&lt;/li&gt;
&lt;li&gt;It gets increasingly obvious, the more I use it.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;a href="https://janusworx.com/tags/pythoncomprehensions/" &gt;Read all about my Python Comprehensions journey here&lt;/a&gt;&lt;/p&gt;</description></item><item><title>A Hundred Days of Code, Day 006 - Starting and Quitting Comprehensions</title><link>https://janusworx.com/work/a-hundred-days-of-code-day-006-starting-and-quitting-comprehensions/</link><pubDate>Mon, 13 Jul 2020 18:21:31 +0530</pubDate><author>feedback@janusworx.com (Mario Jason Braganza)</author><guid>https://janusworx.com/work/a-hundred-days-of-code-day-006-starting-and-quitting-comprehensions/</guid><description>&lt;p&gt;I know I said, I’d start with the basics, Reuven, but please forgive me this once :)&lt;br&gt;
Will do the rest, bottom up :)&lt;/p&gt;
&lt;p&gt;Have started up with this, because I’m &lt;em&gt;fascinated&lt;/em&gt; with how folks manage to build in so much functionality into short, easy to read statements.&lt;br&gt;
To my mind, comprehensions are the pithy proverbs of the programming world.&lt;br&gt;
So here goes …&lt;/p&gt;
&lt;!-- TEASER_END --&gt;

&lt;h3 class="relative group"&gt;Notes
 &lt;div id="notes" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#notes" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;This style of writing code, comes from functional programming&lt;/li&gt;
&lt;li&gt;Functional Programming is another style of writing code
&lt;ul&gt;
&lt;li&gt;It assumes that data is immutable. (Or at least it treats it as such) It does not change the originals I work with. &lt;em&gt;(I assume this means that I always work on a copy or something)&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;we work with sequences of data and create new ones based on the old ones &lt;em&gt;(changing case of a sentence, comes to mind)&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;There are a bunch of techniques/patterns/heuristics that’ll let me do that&lt;/li&gt;
&lt;li&gt;We can treat functions as data and pass them around as arguments to other functions.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;functools&lt;/code&gt; module assists with writing such code&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Why code in such a manner? Some benefits include them being easier to read, easier to debug, shorter, more understandable.&lt;/li&gt;
&lt;li&gt;Distributed Computing / Multiprocessing across processors and cores/threads makes these techniques more relevant in this day and age&lt;/li&gt;
&lt;li&gt;My personal need is to write easy to read code, like when I see a well crafted list comprehension&lt;/li&gt;
&lt;li&gt;If I want to square a list of numbers, my current go to is to generate a new list, using a for loop. Comprehensions are a better way of doing this&lt;/li&gt;
&lt;li&gt;For loops are far when I want to execute something on each item of something iterable, like a list or a dictionary. when i want to have a statement happen&lt;/li&gt;
&lt;li&gt;If just want to transmute the elements of list into something else, change each element, like square them all above, then comprehensions are my friend&lt;/li&gt;
&lt;li&gt;Here’s a list comprehension to square a list of numbers from the list &lt;code&gt;numbers&lt;/code&gt;.
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;[one_number * one_number for one_number in numbers]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;The square brackets make it a list comprehension. If I want dictionary comprehensions, I would abut them with curly braces {}. And if I use (), I’ll get a generator expression (which I know only the bare basics about)&lt;/li&gt;
&lt;li&gt;For loops and comprehensions might look similar, but they really are different&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Note:&lt;/strong&gt; whatever expression you use to generate output has to return something. like the square expression above . because what I want to do is catch all my results in a container. functions (like the &lt;code&gt;print&lt;/code&gt; function) that return &lt;code&gt;None&lt;/code&gt; as their result, cannot work here. They will &lt;em&gt;display&lt;/em&gt; stuff, but store only &lt;code&gt;None&lt;/code&gt; in our comprehension container :)&lt;/li&gt;
&lt;li&gt;I can also write the comprehesion over multiple lines (works because they are bounded by the list brackets here or bounded by other brackets elswhere)&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-python" data-lang="python"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;one_number&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;one_number&lt;/span&gt;		 
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;one_number&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;here with the breakdown of the lines, the first line becomes my output generator; the expression I want to use and the second line, the data I want to process. Much more readable this way&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 class="relative group"&gt;learning / feedback/ experiences from doing exercises
 &lt;div id="learning--feedback-experiences-from-doing-exercises" class="anchor"&gt;&lt;/div&gt;
 
 &lt;span
 class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;
 &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#learning--feedback-experiences-from-doing-exercises" aria-label="Anchor"&gt;#&lt;/a&gt;
 &lt;/span&gt;
 
&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Exercises today required a bit of head scratching and a bit of research, but were relatively simple&lt;/li&gt;
&lt;li&gt;I should keep in mind that the computer can go crazy too. I kept wondering why my code wouldn’t run and and kept banging my head for an hour. Even Rueven’s solution was what I did. Until I restarted my Jupyter note book and everything worked the way I expected.&lt;/li&gt;
&lt;li&gt;Should have a time limit on how much struggling I do. From the last week, it looks like I am a real sucker for pain otherwise. Half an hour tops and then go look up the solution.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Have reached the second set of exercises and realised why Reuven told me to start with the basics. I failed with the second one in this set. I just know, there is a simple solution and I cannot seem to get it. This basically means … yes … that my foundations are creaky.&lt;br&gt;
Note to self, walk before you can run. By Jove, you should’ve learnt that by now! :)&lt;br&gt;
So this is me, waving goodbye sorrowfully to comprehensions.&lt;br&gt;
Will start with the basic Python course tomorrow.&lt;br&gt;
But I’ll be back soon enough, damn it!&lt;/p&gt;
&lt;p&gt;&lt;a href="https://janusworx.com/tags/pythoncomprehensions/" &gt;Read all about my comprehensions journey here&lt;/a&gt;&lt;/p&gt;</description></item></channel></rss>