You could use the or
operator:
return x or "default"
Note that this also returns "default"
if x
is any falsy value, including an empty list, 0, empty string, or even datetime.time(0)
(midnight).
More Related Contents:
- What is the best way to remove accents (normalize) in a Python unicode string?
- How to change the order of DataFrame columns?
- “for line in…” results in UnicodeDecodeError: ‘utf-8’ codec can’t decode byte
- InvalidArgumentException: Message: invalid argument: user data directory is already in use error using –user-data-dir to start Chrome using Selenium
- Is there a way to list pip dependencies/requirements?
- Sorting a dictionary by value then key
- How do I fit a sine curve to my data with pylab and numpy?
- Most efficient way to find mode in numpy array
- How can I check if character in a string is a letter? (Python)
- Writing a list to a file with Python, with newlines
- How can I convert each item in the list to string, for the purpose of joining them? [duplicate]
- How to concatenate two dataframes without duplicates?
- How can I tell if a string repeats itself in Python?
- Monkey-patch Python class
- python pip on Windows – command ‘cl.exe’ failed
- Python read file as stream from HDFS
- Invalid syntax when using “print”? [duplicate]
- What is causing this AttributeError?
- Efficiently Calculating a Euclidean Distance Matrix Using Numpy
- English grammar for parsing in NLTK
- Merging/adding lists in Python
- Why is `print(object)` displaying “?
- open read and close a file in 1 line of code
- Is having a concurrent.futures.ThreadPoolExecutor call dangerous in a FastAPI endpoint?
- What does “TypeError: ‘float’ object cannot be interpreted as an integer” mean when using range?
- Running django tutorial tests fail – No module named polls.tests
- virtualenv: Specifing which packages to use system-wide vs local [duplicate]
- Same name for classmethod and instancemethod
- UnicodeDecodeError when reading CSV file in Pandas
- Run manage.py from AWS EB Linux instance