ast.literal_eval()
ast.literal_eval() is a Python function in the AST Module category. Safely evaluates a string containing a Python literal (list, dict, tuple, number, string, bool, None). The syntax is ast.literal_eval(node_or_string). Parameters: node_or_string. Returns: Python literal value. A typical example: import ast
ast.literal_eval("[1, 2, 3]") # [1, 2, 3]
ast.literal_eval("{a: 1, b: 2}") # {"a": 1, "b": 2}
ast.literal_eval("42") # 42
# Safe: does NOT evaluate arbitrary code. A close sibling is ast.parse(), which parses Python source code into an AST (Abstract Syntax Tree). A close sibling is ast.dump(), which returns a formatted string representation of an AST node. A close sibling is ast.walk(), which recursively yields all descendant AST nodes (depth-first). A close sibling is ast.get_source_segment(), which returns the source code segment corresponding to an AST node. More about this category: Abstract Syntax Tree — literal_eval, parse, dump, walk. Related AST Module entries: ast.parse(), ast.dump(), ast.walk(), ast.get_source_segment(), ast.fix_missing_locations(), ast.copy_location().