Menu Close

How to Convert YAML to Dictionary in Python

How to convert yaml to dictionary in Python

In this article, we are going to see how to convert YAML to Dictionary in Python with the help of examples. Most of the time as a developer your requirement is that convert any YAML file to a Python dictionary, Then in that situation, you have an option to convert any YAML document or any YAML file to a Python dictionary. Python provides an external module pyyaml that is capable of converting YAML to Dictionary.

Prerequisites

Python provides two types of packages first is built-in packages that come with Python by default and the second is external packages that are installed using the pip command. So, to convert YAML to Dictionary in Python we will use pyyaml packages that come under the external package that means you have to install it by using the pip command.

pip install pyyaml

What is YAML

YAML stands for Yet Another Markup Langauge. It is a human-readable data serialization language that is mainly used for writing configuration files where data is being stored or transmitted.
It is becoming very popular in the last few years because it provides an intuitive serialize manner to make build configuration files.
You can see below the document that is an example of YAML.

YAML Document:


name: "Vishvajit"
age: 23
address: Noida
Occupation: Software Developer
Skills:
- Python
- Django
- Flask
- FastAPI
- DRF ( Django Rest Framework )

How to Convert YAML to Dictionary in Python

Here we will convert YAML to Python Dictionary in two ways first it uses a simple YAML string and another is from the YAML file.

Convert YAML to Python Dictionary

In this section, we will see the conversion of the YAML string to a Python dictionary.

Example: Convert YAML to Python Dictionary


import yaml
from pprint import pprint

#yaml document
yaml_document = """
name: "Vishvajit"
age: 23
address: Noida
Occupation: Software Developer
Skills:
- Python
- Django
- Flask
- FastAPI
- DRF ( Django Rest Framework )
"""

# convert yaml document to dict
data = yaml.load(yaml_document, Loader=yaml.Loader)
pprint(data)

Output


{'Occupation': 'Software Developer',
 'Skills': ['Python', 
            'Django', 
            'Flask',  
            'FastAPI',
            'DRF ( Django Rest Framework )'],
 'address': 'Noida',
 'age': 23,
 'name': 'Vishvajit'}

Convert YAML file to Python Dictionary

In this section, you will see how to convert the YAML file to Dictionary in Python using Python pyyaml package.

Students.yaml


---
data:
- name: "Vishvajit"
  age: 23
  address: Noida
  Occupation: Software Developer
  Skills:
  - Python
  - Django
  - Flask
  - FastAPI
  - DRF ( Django Rest Framework )
- name: "John"
  age: 30
  address: UK
  Occupation: front-end Developer
  Skills:
  - HTML
  - JavaScript
  - React
  - Angular
  - Nodejs

Example: Convert YAML file to Python Dictionary


import yaml
from pprint import pprint

with open("students.yaml", "r") as file:
    data = yaml.load(file, Loader=yaml.FullLoader)
    pprint(data)

Output


{'data': [{'Occupation': 'Software Developer',
           'Skills': ['Python',
                      'Django',
                      'Flask',
                      'FastAPI',
                      'DRF ( Django Rest Framework )'],
           'address': 'Noida',
           'age': 23,
           'name': 'Vishvajit'},
          {'Occupation': 'front-end Developer',
           'Skills': ['HTML', 'JavaScript', 'React', 'Angular', 'Nodejs'],
           'address': 'UK',
           'age': 30,
           'name': 'John'}]}

Conclusion

So, In this article, we have seen all about how to convert YAML to Dictionary in Python. Most of the time as a developer you have a YAML file and you want to convert it into Python native data type dictionary, Then you can follow the above approach.

I hope this article will help you to convert YAML to Python Dictionary, If you like this article, please share and keep visiting for further Python tutorials.

PyYaml documentation:- Click Here

Thanks for reading. 👏👏

Previous Article
Next Article

Related Posts