unite 2 strings from array Python
<p>I want to unite 2 strings from an array extracted from a text file:</p>

<pre><code>for n in arrayhere:
    for i in arrayhere:
        newvariable = n+i
</code></pre>

<p>I also tried <code>x.split()</code> in both for loops and <code>str(n+i)</code>.</p>

<p>When I print <code>newvariable</code> or write it on the text file, variables are printed or written not in the same line.</p>

<p>Add this line to your code prior to looping through the elements in your list (they're not called arrays in python, but i'll refer to the list as <code>arrayhere</code> just like you did so you can substitute the code easily):</p>

<pre><code>arrayhere = [x.strip('\n') for x in arrayhere]
</code></pre>

<p>This will strip the new line characters from the elements in your list. The <code>\n</code> at the end of each element are what's causing your <code>newvariable = n + i</code> to print on separate lines.</p>

<p>This would be an ideal way to read your text file into the list:</p>

<pre><code>with open(/path/to/file) as f:
    arrayhere = f.readlines()
arrayhere = [x.strip('\n') for x in arrayhere]
</code></pre>

<p>... and then you can do as you were previously doing:</p>

<pre><code>for n in arrayhere:
    for i in arrayhere:
        newvariable = n + i
</code></pre>

python arrays string for-loop