How to keep specific element indexs in an array
<p>Suppose I have two arrays:</p>

<pre><code>a = [1, 4, 9, 16, 25, 36, 49, 64, 81]
b = [2,5,7]
</code></pre>

<p>And I want to keep the elements indexed in a that are listed in b so the 2nd, 5th and 7th index of a:</p>

<pre><code>a_new = [4, 25, 49]
</code></pre>

<p>I will then plot b against a_new / perform analysis on it.</p>

<p>For my application, a is a long series of simulated data, and b is a time series of when I want to sample from this data.</p>

<p>Thanks</p>

<p>First remember that the first element of an array (or in this case a list) is number 0 and not number 1:</p>

<pre><code>a = [1, 4, 9, 16, 25, 36, 49, 64, 81]
a[0] = 1   # How to adress the first element
a[1] = 4   # How to adress the second element
a[2] = 9   # ...
</code></pre>

<p>So the elements you want (as specified in the array b) are:</p>

<pre><code>a[1] = 4    # a[1] is the same as a[2 - 1] (note that b[0] = 2)
a[4] = 25   # a[4] is the same as a[5 - 1] (note that b[1] = 5)
a[6] = 49   # a[6] is the same as a[7 - 1] (note that b[2] = 7)
</code></pre>

<p>So you can also access the elements this way:</p>

<pre><code>a[ b[0] - 1 ] = 4    # Same as a[1] which is the second element
a[ b[1] - 1 ] = 25   # Same as a[4] which is the fifth element
a[ b[2] - 1 ] = 49   # Same as a[6] which is the seventh element
</code></pre>

<p>This can be wrapped up in a for-loop:</p>

<pre><code>a_new = []    # Start with an empty list
for index in b:   # index in b are all elements in b, thus: b[0] = 2, b[1] = 5 and b[2] = 7
    a_new.append(a[ index - 1])
</code></pre>

<p>This loop will put the elements <code>a[2 - 1]</code> (4), <code>a[5 - 1]</code> (25) and <code>a[7 - 1]</code> (49) into the list<code>a_new</code>.
<br><br>
But there is a shorter way to write that loop:</p>

<pre><code>a_new = [ a[ index - 1] for index in b ]
</code></pre>

<p>Basically, you say <code>a_new = [ ... ]</code>, so <code>a_new</code> is a list, and the <code>...</code> inside will specify, what the list will be filled with. In this case, it will be the elements that the for-loop produces, note that <code>a[ index - 1] for index in b</code> is  the same for-loop as in the first example, written in a compact way.
<br><br>
<strong>What if you get an list index out of range error?</strong>
<br>
Your list<code>a</code> contains 9 elements, so the first element is <code>a[0]</code> and the last is <code>a[8]</code>. If you try to access any other element in a list, for example <code>a[12]</code>, you will get a "list index out of range" error.
<br>
That means: the list b should only contain numbers between 1 and 9 (the length of the list, which you can find out this way <code>len[a] = 9</code>).
<br><br>
<strong>I would recommend</strong>, that you change your list b to <code>b = [1, 4, 6]</code>, since the fifth element of an array is actually adressed like <code>a[4]</code> and not <code>a[5]</code>.
<br>
The code will be a bit easier:</p>

<pre><code>a_new = [ a[index] for index in b ]
</code></pre>

<p>If you don't want errors to happen, the values in b should then be between 0 and 8 (which is <code>len(a) - 1</code>), since <code>a[0]</code> is the first and <code>a[8]</code> is the last element, and only elements between that exist!</p>

python arrays list indexing