Python generate and return tuples in a non-list form
<pre><code>def tuple_gen(x, y, num):
    pos = []
    for i in range(num):
        x += 1
        pos.append([x, y])
        pos[i] = tuple(pos[i])
    return pos
</code></pre>

<p>my input is:</p>

<pre><code>&gt;&gt;&gt; tuple_gen(0,1,5)
[(1, 1), (2, 1), (3, 1), (4, 1), (5, 1)]
</code></pre>

<p>What I want is to return tuples but not a list:</p>

<pre><code>&gt;&gt;&gt; tuple_gen(0,1,5)
(1, 1), (2, 1), (3, 1), (4, 1), (5, 1)
</code></pre>

<p>Because I want to form a list that includes different kind of tuples:</p>

<pre><code>&gt;&gt;&gt; [tuple_gen(0,1,5), tuple_gen(0,2,6)]
[(1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (1, 2), (2, 2), (3, 2), (4, 2), (5, 2), (6, 2)]
</code></pre>

<p>I want a short and clearest solution.</p>

<p>*note that I don't want to use any <code>extend</code> or <code>append</code> to merge those lists because I need so many tuples</p>

<p>You just need to change the way you're calling <code>tuple_gen</code>. Here's an example, with a slightly improved version of <code>tuple_gen</code>:</p>

<pre><code>def tuple_gen(x, y, num):
    pos = []
    for i in range(num):
        x += 1
        pos.append((x, y))
    return pos

bases = (
    (0, 1, 5), 
    (0, 2, 6),
)

a = [t for args in bases for t in tuple_gen(*args)] 
print(a)
</code></pre>

<p><strong>output</strong></p>

<pre><code>[(1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (1, 2), (2, 2), (3, 2), (4, 2), (5, 2), (6, 2)]
</code></pre>

<p>An alternative is to create a generator that yields all the desired tuples:</p>

<pre><code>def make_tuples(bases):
    for x, y, num in bases:
        yield from ((i, y) for i in range(x + 1, num + x + 1))

bases = (
    (0, 1, 5), 
    (0, 2, 6),
)

a = list(make_tuples(bases))
print(a)
</code></pre>

<p>Depending on your actual needs, you may be able to avoid creating a list (thus saving RAM) and just use the tuples as they are created. Eg,</p>

<pre><code>for t in make_tuples(bases):
    print(t)
</code></pre>

<p><strong>output</strong></p>

<pre><code>(1, 1)
(2, 1)
(3, 1)
(4, 1)
(5, 1)
(1, 2)
(2, 2)
(3, 2)
(4, 2)
(5, 2)
(6, 2)
</code></pre>

python arrays list tuples unpack