title: Difference between Bytearray and List in Python
body: <p>I am curious to know how memory management differs between Bytearray and list in Python.</p>
  
  <p>I have found a few questions like <a href="https://stackoverflow.com/questions/30145490/difference-between-bytearray-and-list">Difference between bytearray and list</a> but not exactly answering my question. </p>
  
  <p>My question precisely ...</p>
  
  <pre><code>from array import array
  &gt;&gt;&gt; x = array("B", (1,2,3,4))
  &gt;&gt;&gt; x.__sizeof__()
  36
  &gt;&gt;&gt; y = bytearray((1,2,3,4))
  &gt;&gt;&gt; y.__sizeof__()
  32
  &gt;&gt;&gt; z = [1,2,3,4]
  &gt;&gt;&gt; z.__sizeof__()
  36
  </code></pre>
  
  <p>As we can see there is a difference in sizes between list/array.array (36 bytes for 4 elements) and a byte array (32 bytes for 4 elements). Can someone explain to me why is this? It makes sense for byte array that it is occupying <code>32</code> bytes of memory for <code>4</code> elements <code>( 4 * 8 == 32 )</code>, but how can this be interpreted for list and array.array? </p>
  
  <pre><code># Lets take the case of bytearray ( which makes more sense to me at least :p)
  for i in y:
          print(i, ": ", id(i))
  
  1 :  499962320
  2 :  499962336 #diff is 16 units
  3 :  499962352 #diff is 16 units
  4 :  499962368 #diff is 16 units
  </code></pre>
  
  <p>Why does the difference between two contiguous elements differ by <code>16</code> units here, when each element occupies only <code>8</code> bytes. Does that mean each memory address pointer points to a nibble?</p>
  
  <p>Also what is the criteria for memory allocation for an integer? I read that Python will assign more memory based on the value of the integer (correct me if I am wrong) like the larger the number the more memory.</p>
  
  <p><em>Eg:</em></p>
  
  <pre><code>&gt;&gt;&gt; y = 10
  &gt;&gt;&gt; y.__sizeof__()
  14
  &gt;&gt;&gt; y = 1000000
  &gt;&gt;&gt; y.__sizeof__()
  16
  &gt;&gt;&gt; y = 10000000000000
  &gt;&gt;&gt; y.__sizeof__()
  18
  </code></pre>
  
  <p>what is the criteria that Python allocates memory?</p>
  
  <p>And why Python is occupying so much more memory while <code>C</code> only occupies 8 bytes (mine is a 64 bit machine)? when they are perfectly under the range of integer <code>(2 ** 64)</code> ?</p>
  
  <p><em>Metadata :</em></p>
  
  <p><em>Python version :</em> <code>'3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)]'</code></p>
  
  <p><em>Machine arch :</em> 64-bit</p>
  
  <p><strong>P.S</strong> : Kindly guide me to a good article where Python memory management is explained better. I had spent almost an hour to figure these things out and ended up asking this Question in SO.  :(</p>