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.

Leave a Reply

Your email address will not be published. Required fields are marked *