Question: Is there a way in python to add an object to a list if some condition?

I want to do this:

```
middleware = ["a",
"b",
"c" if DEBUG,
"d"
]

```

Such that if `DEBUG` is False, the list will be ["a", "b", "d"]

My current best suggestion is this:

```
middleware = ["a",
"b"] + \
(["c"] if DEBUG else []) + \
["d"
]

```

which doesn't look very intuitive

Answer: 
The closest you can get is using [the additional unpacking generalizations](https://www.python.org/dev/peps/pep-0448/) to unpack a variable length inner `tuple` (or `list`) based on the result of a conditional `if/else` operator:

```
middleware = ["a",
    "b",
    *(("c",) if DEBUG else ()),  # Or if you like brackets, *(["c"] if DEBUG else [])
    "d"
]

```

By using unpacking, you can select between two different inner sequences to unpack, getting the desired result with minimal changes to your desired syntax.

In the specific case of length one strings, the middle line can be simplified to:

```
    *("c" if DEBUG else ""),

```

or even (if `DEBUG` is a boolean, or a `0` or `>0` integer value):

```
    *"c"[:DEBUG], 

    *"c" * DEBUG, # For boolean or 0/1 integers only

```

and those can be expanded to non-length one strings by wrapping in `list` brackets:

```
    *["abc"][:DEBUG], 

    *["abc"] * DEBUG, # For boolean or 0/1 integers only

```

but basically all of these alternatives fall into the "too clever by half" category of programming tricks (if you find yourself doing lots of math or slicing with `bool`s, you're probably being too clever).
If the objects in the list are all strings as they are in your question, a somewhat less ugly compromise would be to use an f-string with a `split` instead:

```
middleware = f'''
    a
    b
    {'c' * DEBUG}
    d
'''.split()

```

EDIT: Revised to use the multiply operator with inspiration from @superbrain's comment.