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 need to make a copy of the old list in the loop:</p>

<pre><code>input_elements = ["a", "b", "c", "d"]
my_array = ["1", "2", "3", "4"]
new_list = []
for e in input_elements:
  tmp_list = list(my_array)
  tmp_list.append(e)
  new_list.append(tmp_list)

print(new_list)
</code></pre>

<p>Output:</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>Note that <code>tmp_list = list(my_array)</code> makes a new copy of <code>my_array</code>.</p>

python list