Insert elements in lists under array python
<p>I have a array consisting of tuples.</p>

<pre><code>Data = [('1234', 'abcd'), ('5678', 'efgh')]
</code></pre>

<p>I now have another set of variables in an array:</p>

<pre><code>add = ["#happy", "#excited"]
</code></pre>

<p>I'm trying to append 'add' to 'Data' in the same order such that the output should look like:</p>

<pre><code>data_new = [('1234', 'abcd', '#happy'), ('5678', 'efgh',"#excited")]
</code></pre>

<p>Is that possible? </p>

<p>You can use list comprehension with <code>enumerate()</code>:</p>

<pre><code>&gt;&gt;&gt; Data = [('1234', 'abcd'), ('5678', 'efgh')]
&gt;&gt;&gt; add = ['#happy', '#excited']
&gt;&gt;&gt; [x + (add[i],) for i,x in enumerate(Data)]
[('1234', 'abcd', '#happy'), ('5678', 'efgh', '#excited')]
</code></pre>

<p>Note that a common pythonic way to solve this type of problem is with <code>zip()</code>, but it doesn't immediately give the desired output for your example because you end up with nested tuples:</p>

<pre><code>&gt;&gt;&gt; zip(Data,add) # or list(zip(Data,add)) in Python3
[(('1234', 'abcd'), '#happy'), (('5678', 'efgh'), '#excited')]
</code></pre>

python arrays list python-2.7 tuples