# [Tutor] Aschenputtel problem

John Fouhy john at fouhy.net
Thu Sep 15 23:41:37 CEST 2005

```On 16/09/05, Kent Johnson <kent37 at tds.net> wrote:
> Alan Gauld wrote:
> > Bearing in mind that shorter is not necessarily better...
> >
> > [condition(i) and list1.append(i) or list2.append(i) for i in
> > original]
>
> Hmm, no, this will always evaluate list2.append(i) because the value of list1.append(i) is None. You could use
>
> [ (condition(i) and list1 or list2).append(i) for i in original ]

This will not work either, because initially, list1 will be [] which
is false.  So everything will end up in list2.

>>> list1, list2 = [], []
>>> [(i%2==1 and list1 or list2).append(i) for i in range(10)]
[None, None, None, None, None, None, None, None, None, None]
>>> list1, list2
([], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

You can get around this with a dodge..

>>> list1, list2 = [], []
>>> [(i%2 == 1 and [list1] or [list2])[0].append(i) for i in range(10)]
[None, None, None, None, None, None, None, None, None, None]
>>> list1, list2
([1, 3, 5, 7, 9], [0, 2, 4, 6, 8])

But I'm sure that using side-effects in a list comprehension is
prohibited by at least one major religion, so really, I would just
stick to a for loop :-)

--
John.
```