# iterating over list with one mising value

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Feb 8 02:50:56 CET 2012

```(Apologies in advance for breaking threading, but the original post in
this thread doesn't appear for me.)

> On Tue, Feb 7, 2012 at 5:27 AM, Sammy Danso <samdansobe at yahoo.com>
> wrote:
>>
>> Hello experts,
>> I am having trouble accessing the content of my list. my list content
>> has 2-pair value with the exception of one which has single value. here
>> is an example  ['a', 1, 'b', 1, 'c', 3, 'd']
>>
>> I am unable to iterate through list to access invidual value pairs

Here's a recipe to collate a sequence of interleaved items. It collects
every nth item:

from itertools import islice, tee

def collate(iterable, n):
t = tee(iter(iterable), n)
slices = (islice(it, i, None, n) for (i, it) in enumerate(t))
return [list(slice) for slice in slices]

And here it is in use:

>>> seq = [1, 'a', 'A', 2, 'b', 'B', 3, 'c', 'C', 4, 'd', 'D', 5, 'e']
>>> collate(seq, 3)
[[1, 2, 3, 4, 5], ['a', 'b', 'c', 'd', 'e'], ['A', 'B', 'C', 'D']]
>>> collate(seq, 2)
[[1, 'A', 'b', 3, 'C', 'd', 5], ['a', 2, 'B', 'c', 4, 'D', 'e']]
>>> collate(seq, 9)
[[1, 4], ['a', 'd'], ['A', 'D'], [2, 5], ['b', 'e'], ['B'], [3], ['c'],
['C']]

--
Steven

```