editing and reordering tuples in a list
<p>I have a list of tuples:</p>

<pre><code>lst = [(1, "text"), (2, "more"), (5, "more"), (10, "more")]
</code></pre>

<p>The tuples have the structure <code>(int, string)</code> and start from 1 and the max value is 10. I would like to edit and reorder them to the following:</p>

<pre><code>lst2 = [(1, "text"), (2, "more"), (3, ""), (4, ""), (5, "more"), (6, ""), (7, ""), (8, ""),
       (9, ""), (10, "more")]
</code></pre>

<p>As you can see I want to create a new list that is consecutively numbered up to 10. All int's from the tuples in the first list <code>lst</code> that doesn't occur in the range of 1 to 10 will produce an empty string in the new list <code>lst2</code>.</p>

<p>I came up with this code:</p>

<pre><code>lst2 = []
for tupl in lst:
  for k in range(1,11):
    if tupl[0] == k:
      lst2.append((k, tupl[1]))
    else:
      lst2.append((k, ""))
print lst2
</code></pre>

<p>however the result is weird:</p>

<pre><code>[(1, 'text'), (2, ''), (3, ''), (4, ''), (5, ''), (6, ''), (7, ''), (8, ''), (9, ''),
(10, ''), (1, ''), (2, 'more'), (3, ''), (4, ''), (5, ''), (6, ''), (7, ''), (8, ''),
(9, ''), (10, ''), (1, ''), (2, ''), (3, ''), (4, ''), (5, 'more'), (6, ''), (7, ''),
(8, ''), (9, ''), (10, ''), (1, ''), (2, ''), (3, ''), (4, ''), (5, ''), (6, ''), (7, ''),
(8, ''), (9, ''), (10, 'more')]
</code></pre>

<p>Can anyone please help me or tell me what I am doing wrong? Thanks.</p>

<p>There are already better answers, but just to show you how your loop could be modified to yield the desired result:</p>

<pre><code>for k in range(1,11):
  exists=False
  for tupl in lst:
    if tupl[0] == k:
      lst2.append((k, tupl[1]))
      exists = True
      break
  if not exists:
    lst2.append((k, ""))
print lst2
</code></pre>

python