Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally add items to a list when defining the list?

Is there a way to add items to a list conditionally, when defining the list? Here's what I mean:

l = [
    Obj(1),
    Obj(2),
    Separator() if USE_SEPARATORS,
    Obj(3),
    Obj(4),
    Obj(5),
    Separator() if USE_SEPARATORS,
    Obj(6)
]

Obviously the above code doesn't work, but is there a similar way?

Currently I have

l = [item for item in (
        Obj(1),
        Obj(2),
        Separator() if USE_SEPARATORS,
        Obj(3),
        Obj(4),
        Obj(5),
        Separator() if USE_SEPARATORS,
        Obj(6)
) if not isinstance(item, Separator) or USE_SEPARATORS]

But I was wondering if there's an other way that wouldn't required looping through the list, since they can be 10 000 items long and the server stops for a quarter second or so when I execute the code. It's for a first-person shooter game, so quarter second might actually have an effect on someone dying or living.

like image 506
Markus Meskanen Avatar asked Mar 05 '15 13:03

Markus Meskanen


3 Answers

Another approach is to use the splat/unpacking operator to expand an element either to the separator or to nothing:

possible_separator = (Separator(),) if USE_SEPARATORS else ()
l = [
    Obj(1),
    Obj(2),
    *possible_separator,
    Obj(3),
    Obj(4),
    Obj(5),
    *possible_separator,
    Obj(6)
]
like image 160
jamesdlin Avatar answered Nov 19 '22 23:11

jamesdlin


I'd just insert them afterwards; lists are mutable after all:

l = [
    HeadObj(1),
    HeadObj(2),
    BodyObj(1),
    BodyObj(2),
    BodyObj(3),
    FooterObj(1)
]
if USE_SEPARATORS:
    l.insert(2, Separator())
    l.insert(6, Separator())
like image 40
Martijn Pieters Avatar answered Nov 20 '22 00:11

Martijn Pieters


I'd add separators and remove them if not needed:

l = [
    Obj(1),
    Obj(2),
    Separator(),
    Obj(3),
    Obj(4),
    Obj(5),
    Separator(),
    Obj(6)]

if not USE_SEPARATORS:
    l = [object for object in l if not isinstance(object, Separator)]
like image 2
Peter Wood Avatar answered Nov 20 '22 00:11

Peter Wood