Feb-28-2017, 08:15 PM
Aside from just using
I a little bit don't like if/else used like this, because it isn't obvious at a glance that there's *always* something that happens, every single iteration, so I'd write it like this:
while numbers
, or just using a for loop, what else isn't pythonic about it?I a little bit don't like if/else used like this, because it isn't obvious at a glance that there's *always* something that happens, every single iteration, so I'd write it like this:
>>> numbers = [12, 37, 5, 42, 8, 3] >>> separated = {'odd': [], 'even': [] } >>> for num in numbers: ... key = 'even' if num % 2 == 0 else 'odd' ... separated[key].append(num) ... >>> separated {'odd': [37, 5, 3], 'even': [12, 42, 8]}But I don't know if that's any more or less pythonic than just using two different lists.