AJW - 5 months ago 37

Python Question

I am a python newbie and I am trying to learn how to "zip" lists. To this end, I have a program, where at a particular point, I do the following:

`x1, x2, x3 = stuff.calculations(withdataa)`

This gives me three lists,

`x1`

`x2`

`x3`

Now, I do:

`zipall = zip(x1, x2, x3)`

However, when I do:

`print "len of zipall %s" % len(zipall)`

I get 20, which is not what I expected. I expected three. I think I am doing something fundamentally wrong.

Answer

When you `zip()`

together three lists containing 20 elements each, the result has twenty elements. Each element is a three-tuple.

See for yourself:

```
In [1]: a = b = c = range(20)
In [2]: zip(a, b, c)
Out[2]:
[(0, 0, 0),
(1, 1, 1),
...
(17, 17, 17),
(18, 18, 18),
(19, 19, 19)]
```

To find out how many elements each tuple contains, you could examine the length of the first element:

```
In [3]: result = zip(a, b, c)
In [4]: len(result[0])
Out[4]: 3
```

Of course, this won't work if the lists were empty to start with.