How can I append this elements to an array in python?
<pre><code>input_elements = ["a", "b", "c", "d"]
my_array = ["1", "2", "3", "4"]
</code></pre>

<p>the output I want is:</p>

<pre><code>["1", "2", "3", "4", "a"]
["1", "2", "3", "4", "b"]
["1", "2", "3", "4", "c"]
["1", "2", "3", "4", "d"]
</code></pre>

<p>I tried:</p>

<pre><code>for e in input_elements:
  my_array.append(e)
</code></pre>

<p>I know the code right above is wrong, so I am wondering how I can generate the output like that.</p>

<p>You can use a <a href="https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions" rel="nofollow">list comprehension</a> to solve your issue. </p>

<pre><code>&gt;&gt;&gt; input_elements = ["a", "b", "c", "d"]
&gt;&gt;&gt; my_array = ["1", "2", "3", "4"]
&gt;&gt;&gt; [my_array+[i] for i in input_elements]
</code></pre>

<p>The result looks like</p>

<pre><code>&gt;&gt;&gt; from pprint import pprint
&gt;&gt;&gt; pprint([my_array+[i] for i in input_elements])
[['1', '2', '3', '4', 'a'],
 ['1', '2', '3', '4', 'b'],
 ['1', '2', '3', '4', 'c'],
 ['1', '2', '3', '4', 'd']]
</code></pre>

<p>See <a href="http://stackoverflow.com/questions/34835951/what-does-list-comprehension-in-python-mean-how-does-it-work-and-how-can-i-us">What does &quot;list comprehension&quot; in Python mean? How does it work and how can I use it?</a> for more details about them. </p>

python list