import ast
L = ["hello", "3", "3.64", "-1"]
def tryeval(val):
try:
val = ast.literal_eval(val)
except ValueError:
pass
return val
print [tryeval(x) for x in L]
More Related Contents:
- Function to return results in a dictionary in Python
- Creating a simple XML file using python
- list to dictionary conversion with multiple values per key?
- Convert UTF-8 with BOM to UTF-8 with no BOM in Python
- Efficient calculation of Fibonacci series
- using requests with TLS doesn’t give SNI support
- Python’s json module, converts int dictionary keys to strings
- Simulate Python keypresses for controlling a game
- Powersets in Python using itertools
- Spark DataFrame: Computing row-wise mean (or any aggregate operation)
- How to download a file from Google Drive using Python and the Drive API v3
- How do I set sys.argv so I can unit test it?
- Running a bash script from a cronjob fails with “No such file or directory”
- Pandas unstack problems: ValueError: Index contains duplicate entries, cannot reshape
- Serializing a Python namedtuple to json
- Python: format string with custom delimiters [duplicate]
- How to convert a dataframe to a dictionary
- Non-lazy evaluation version of map in Python3?
- Why does using multiplication operator on list create list of pointers? [duplicate]
- Filling missing values using forward and backward fill in pandas dataframe (ffill and bfill)
- Fastest Way to Delete a Line from Large File in Python
- What does return mean in Python? [closed]
- How to convert a column or row matrix to a diagonal matrix in Python?
- Convert NumPy array to 0 or 1 based on threshold
- Stroke Width Transform (SWT) implementation (Python)
- Make @lru_cache ignore some of the function arguments
- Catching exception in context manager __enter__()
- PyCharm hangs on ‘scanning files to index’ background task
- Beautiful Soup and Table Scraping – lxml vs html parser
- How to install multiple python packages at once using pip