Does python perform tasks on lists in parallel by itself?
<p>I stumbled upon this piece of work:</p>

<pre><code>def getChild(self, childName):
    for child in self.children :
        if(childName == child.data['name']):
            return child
    return None
</code></pre>

<p>As far as ranting is concerned this code implies everything there is to say and know about this library.</p>

<p>I am not that deep into python but I afaik the canonic pythonic way of going about this task is</p>

<pre><code>def getChild(self, childName):
    return (
        [ child for child in self.children
            if child.data['name'] == childName
        ] + [ None ]
        )[0]
</code></pre>

<p>or, for better readability,</p>

<pre><code>def getChild(self, childName):
    myChildren = dict([
         (child.data['name'],child)
             for child in self.children
    ])
    try:
        return myChildren[childName]
    except:
        return None
</code></pre>

<p>Besides the fact that I still have no clue about how and when to indent and where to put <code>{</code>,<code>[</code>,<code>(</code>,<code>)</code>,<code>]</code> and <code>}</code>, my alternatives always operate on all elements of <code>self.children</code>.</p>

<p>Does python recognise this and work through the data sets in parallel, or will it still work through the array sequentially? (and therefore always take longer than the simple search I found in that library)</p>

<p>With the amount of lookups performed it is of course better to change the library to always maintain a dictionary of children instead of a list, but that is besides the point. I would like to know how python interprets and handles this code. </p>

<p>No, there is no automatic parallelization. Python is about writing your code efficiently, but not about making it computationally efficient. And there is a bigger problem: the GIL - Global Interpreter Lock. That means that only one thread at a time is executed. So there is no big point in parallelization of CPU intensive tasks in python. There are interpreters without GIL, but general rule of thumb: if you don't know about GIL, then you have it.</p>

<p>Concerning the code you've posted: python allows you to write in the most common procedural style or in functional style, beloved in universities. You can choose any.</p>

python list dictionary parallel-processing