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: 
I don't think there's any simpler inline way to do it.

Just write a straightforward `if` statement:

```
middleware = ["a", "b"]
if DEBUG:
    middleware.append("c")
middleware.append("d")

```

or

```
middleware = ["a", "b", "d"]
if DEBUG:
    middleware.insert(2, "c")

```

Another option would be to build the `middleware` list from another list that controls it.

```
mware_with_options = [
    {"name": "a", "type": "always"}, 
    {"name": "b", "type": "always"}, 
    {"name": "c", "type": "debug"}, 
    {"name": "d", "type": "always"}
]
middleware = [item['name'] for item in mware_with_options if item['type'] = "always" or (DEBUG and item['type'] = "debug")]

```
If the items in the list are all truthy values, you can use the `and` operator to set the item to `False` if `DEBUG` is `False`, and then filter the falsey items from the list afterwards:

```
middleware = [
    "a",
    "b",
    DEBUG and "c",
    "d"
]
middleware[:] = filter(None, middleware)

```

or,

```
*middleware, = filter(None, [
    "a",
    "b",
    DEBUG and "c",
    "d"
])

```