Serialize and Deserialize Python Tuples to YAML in Python
In this tutorial, you will learn how to convert Python tuples to YAML and deserialize YAML back into Python tuples.
You’ll also handle nested and complex tuple structures.
Serialize Python tuples to YAML Using PyYAML
Convert Python tuples into YAML format for storage or transmission using PyYAML.
import yaml data = { 'user': 'Ahmed', 'coordinates': (29.9753, 31.1376), 'languages': ('Arabic', 'English', 'French') } yaml_output = yaml.dump(data, default_flow_style=False) print(yaml_output)
Output:
coordinates: !!python/tuple - 29.9753 - 31.1376 languages: !!python/tuple - Arabic - English - French user: Ahmed
The tuples in the dictionary are represented with the !!python/tuple
tag, which is specific to PyYAML to indicate that these are tuples, not lists.
Deserialize YAML to Python tuples
To load YAML data with custom tags like !!python/tuple
, you can use yaml.Loader
:
import yaml yaml_data = """ coordinates: !!python/tuple - 29.9753 - 31.1376 languages: !!python/tuple - Arabic - English - French user: Ahmed """ data = yaml.load(yaml_data, Loader=yaml.Loader) print(data)
Output:
{'coordinates': (29.9753, 31.1376), 'languages': ('Arabic', 'English', 'French'), 'user': 'Ahmed'}
Handle complex tuple structures in YAML
Tuples containing lists
To serialize tuples with lists inside them to YAML, you can use the yaml
library:
import yaml data = { 'user': 'Ahmed', 'coordinates': (29.9753, 31.1376), 'languages': ('Arabic', 'English', 'French'), 'preferences': ( ['reading', 'traveling'], ['coding', 'music'] ) } yaml_output = yaml.dump(data, default_flow_style=False) print(yaml_output)
Output:
coordinates: !!python/tuple - 29.9753 - 31.1376 languages: !!python/tuple - Arabic - English - French preferences: !!python/tuple - - reading - traveling - - coding - music user: Ahmed
Each list is serialized as a block sequence in YAML.
Tuples containing dictionaries
In the same way, you can use the yaml
library:
import yaml data = { 'user': 'Ahmed', 'coordinates': (29.9753, 31.1376), 'languages': ('Arabic', 'English', 'French'), 'preferences': ( {'hobby': 'reading', 'frequency': 'daily'}, {'hobby': 'traveling', 'frequency': 'monthly'} ) } yaml_output = yaml.dump(data, default_flow_style=False) print(yaml_output)
Output:
coordinates: !!python/tuple - 29.9753 - 31.1376 languages: !!python/tuple - Arabic - English - French preferences: !!python/tuple - frequency: daily hobby: reading - frequency: monthly hobby: traveling user: Ahmed
Convert YAML to list of tuples
Transform YAML data into a list of tuples for easier manipulation and access in Python.
import yaml yaml_input = """ students: - [Ali, 85] - [Fatma, 92] - [Hassan, 78] """ data = yaml.safe_load(yaml_input) students = [tuple(student) for student in data['students']] print(students)
Output:
[('Ali', 85), ('Fatma', 92), ('Hassan', 78)]
Here we convert each list to a tuple using the tuple()
method.
Mokhtar is the founder of LikeGeeks.com. He is a seasoned technologist and accomplished author, with expertise in Linux system administration and Python development. Since 2010, Mokhtar has built an impressive career, transitioning from system administration to Python development in 2015. His work spans large corporations to freelance clients around the globe. Alongside his technical work, Mokhtar has authored some insightful books in his field. Known for his innovative solutions, meticulous attention to detail, and high-quality work, Mokhtar continually seeks new challenges within the dynamic field of technology.