iterate over elements in the tuple and recursively flatten it
alternative is - to convert to str, remove all brackets and use ast.literal_eval to convert back to tuple
spam = (((2517.0, 2525.0), 2437.0), 2440.0) def flatten(arg): flat_list = [] for item in arg: if isinstance(item, (tuple, list)): flat_list.extend(flatten(item)) else: flat_list.append(item) return tuple(flat_list) print(flatten(spam))
alternative is - to convert to str, remove all brackets and use ast.literal_eval to convert back to tuple
from ast import literal_eval spam = (((2517.0, 2525.0), 2437.0), 2440.0) spam = str(spam).replace('(', '').replace(')', '') spam = literal_eval(spam) print(spam)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs