Python: a += b not the same as a = a + b
<blockquote>
  <p><strong>Possible Duplicate:</strong><br>
  <a href="http://stackoverflow.com/questions/2347265/what-does-plus-equals-do-in-python">What does plus equals (+=) do in Python?</a>  </p>
</blockquote>



<p>I found an interesting "feature" of the python language today, that gave me much grief.</p>

<pre><code>&gt;&gt;&gt; a = [1, 2, 3]
&gt;&gt;&gt; b = "lol"
&gt;&gt;&gt; a = a + b 
TypeError: can only concatenate list (not "str") to list
&gt;&gt;&gt; a += b
&gt;&gt;&gt; a
[1, 2, 3, 'l', 'o', 'l']
</code></pre>

<p>How is that? I thought the two were meant to be equivalent! Even worse, this is the code that I had a hell of a time debugging</p>

<pre><code>&gt;&gt;&gt; a = [1, 2, 3]
&gt;&gt;&gt; b = {'omg': 'noob', 'wtf' : 'bbq'}
&gt;&gt;&gt; a = a + b
TypeError: can only concatenate list (not "dict") to list
&gt;&gt;&gt; a += b
&gt;&gt;&gt; a
[1, 2, 3, 'omg', 'wtf']
</code></pre>

<p>WTF! I had lists and dicts within my code, and was wondering how the hell I ended up appending the keys of my dict onto a list without ever calling .keys(). As it turns out, this is how.</p>

<p>I thought the two statements were meant to be equivalent. Even ignoring that, I can kind of understand the way you append strings onto lists (since strings are just character arrays) but dictionaries? Maybe if it appended a list of (key, value) tuples, but grabbing only the keys to add to the list seems completely arbitrary.</p>

<p>Does anyone know the logic behind this?</p>
