# [Tutor] a list of tuples with varying lengths

Peter Otten __peter__ at web.de
Wed Jan 19 04:10:46 EST 2022

```On 19/01/2022 07:20, Phil wrote:
> I hope the following makes sense to you and I'm not sure that I should
> even post such a vague question.
>
> What I need is s, d to = (0, 4) during the first time interval and then
> s, d = (0, 3) and (1, 4) the second time. There are many time intervals,
> this is just the first two.
>
> My solution is to have a look_up table that looks something like this,
> the first three entries are:
>
> table = [[(0, 4)], [(0, 3), (1, 4)], [(0, 2), (1, 3), (2, 4)]]

That's not a list of tuples -- it's a list of lists of tuples. To
iterate over the tuples you need two nested loops:

>>> table = [[(0, 4)], [(0, 3), (1, 4)], [(0, 2), (1, 3), (2, 4)]]
>>> for row in table:
for field in row:
print(field)

(0, 4)
(0, 3)
(1, 4)
(0, 2)
(1, 3)
(2, 4)

As all tuples have a length of two you can safely unpack them in the
inner loop:

>>> for row in table:
for s, d in row:
print(f"{s=}, {d=}")

s=0, d=4
s=0, d=3
s=1, d=4
s=0, d=2
s=1, d=3
s=2, d=4

By the way, the table you give doesn't look like a "lookup table" which
is typically used to provide a "key" (the list index in your case) and
return a "value" (here a complete row). But you seem to want the
individual tuples, and all of them.

Perhaps you'd like to explain the problem you are trying solve?
Some of us might be willing clear things up a bit for you ;)

> The first entry has 1 tuple the second entry has 2 tuples the third has
> 3 tuples, etc
>
> My first thought was to extract the tuple values with a for-loop with a
> variable range, like this: for s, d in range(len(table[index])) but that
> idea ended in failure. I cannot see a simple way to calculate the values
> even though there is a pattern and so a look-up table seems like a good
> idea.
>
> I've spent days on this and have made little progress so any ideas will
> be greatly appreciated.
>

```