I’m pretty regularly in the situation of making an inline list, and having an entry in the list that I want to include conditionally. For example:
verbose: bool = ...
subprocess.run(['cmd', '--verbose' if verbose else ...])
Usually I end up making the list separately:
args = ['cmd']
if verbose:
args.append('--verbose')
Or sometimes if it keeps coming up I’ll use a placeholder and filter it out:
subprocess.run([arg for arg in ['cmd', '--verbose' if verbose else None] if arg is not None])
But those are both clunky. Has there ever been a proposal for a way to specify an element during list construction that is just skipped entirely? My first instinct is the pass
keyword, e.g.:
subprocess.run(['cmd', '--verbose' if verbose else pass])
In general it would be the case that:
[1, pass, 2, pass, 3] == [1, 2, 3]
I expect something similar would also work in other inline constructions:
('yay', pass, 'tuples')
{'also': True, 'dicts': pass}
{'why', 'not', pass, 'sets'}
I don’t know the technical implications of using pass
specifically and don’t care so much about the exact syntax, as long as it’s relatively simple compared to the workarounds I mentioned using above. Not sure if this has already been suggested, I was struggling to come up with good search keywords.