How to insert string to each token of a list of strings?
<p>Lets assume I have the following list:</p>

<pre><code>l = ['the quick fox', 'the', 'the quick']
</code></pre>

<p>I would like to transform each element of the list into a url as follows:</p>

<pre><code>['&lt;a href="http://url.com/the"&gt;the&lt;/a&gt;', '&lt;a href="http://url.com/quick"&gt;quick&lt;/a&gt;','&lt;a href="http://url.com/fox"&gt;fox&lt;/a&gt;', '&lt;a href="http://url.com/the"&gt;the&lt;/a&gt;','&lt;a href="http://url.com/the"&gt;the&lt;/a&gt;', '&lt;a href="http://url.com/quick"&gt;quick&lt;/a&gt;']
</code></pre>

<p>So far, I tried the following:</p>

<pre><code>list_words = ['&lt;a href="http://url.com/{}"&gt;{}&lt;/a&gt;'.format(a, a) for a in x[0].split(' ')]
</code></pre>

<p>The problem is that the above list-comprehension just does the work for the first element of the list:</p>

<pre><code>['&lt;a href="http://url.com/the"&gt;the&lt;/a&gt;',
 '&lt;a href="http://url.com/quick"&gt;quick&lt;/a&gt;',
 '&lt;a href="http://url.com/fox"&gt;fox&lt;/a&gt;']
</code></pre>

<p>I also tried with a <code>map</code> but, it didn't work:</p>

<pre><code>[map('&lt;a href="http://url.com/{}"&gt;{}&lt;/a&gt;'.format(a,a),x) for a in x[0].split(', ')]
</code></pre>

<p>Any idea of how to create such links from the tokens of a list of sentences?</p>

<p>In <strong>one-liner</strong>:</p>

<pre><code>list_words = ['&lt;a href="http://url.com/{}"&gt;{}&lt;/a&gt;'.format(a,a) for a in [i for sub in [i.split() for i in l] for i in sub]]
</code></pre>

<p><strong>In steps</strong></p>

<p>You can split the list:</p>

<pre><code>l = [i.split() for i in l]
</code></pre>

<p>and then flatten it:</p>

<pre><code>l = [i for sub in l for i in sub]
</code></pre>

<p>result:</p>

<pre><code>&gt;&gt;&gt; l
['the', 'quick', 'fox', 'the', 'the', 'quick']
</code></pre>

<p>Then:</p>

<pre><code>list_words = ['&lt;a href="http://url.com/{}"&gt;{}&lt;/a&gt;'.format(a,a) for a in l]
</code></pre>

<p>You will finally take:</p>

<pre><code>&gt;&gt;&gt; list_words
['&lt;a href="http://url.com/the"&gt;the&lt;/a&gt;', '&lt;a href="http://url.com/quick"&gt;quick&lt;/a&gt;', '&lt;a href="http://url.com/fox"&gt;fox&lt;/a&gt;', '&lt;a href="http://url.com/the"&gt;the&lt;/a&gt;', '&lt;a href="http://url.com/the"&gt;the&lt;/a&gt;', '&lt;a href="http://url.com/quick"&gt;quick&lt;/a&gt;']
</code></pre>

python string python-3.x list-comprehension