2022-12-31

[Python] Nesting LIST inside DICT / Nesting DICT inside LIST / Nesting DICT inside DICT

#!/usr/bin/env python3

# ---------------------------------------
# Project Name: PythonCheatSheet
# File Name: Dict15-01-Nested-Dict.py
# Created Date : 2022-12-31 08:56:51 UTC+7
# Last Modified: 2022-12-31 09:09:29 UTC+7
# ---------------------------------------
import json
from pytictoc import TicToc
t = TicToc()  # create TicToc instance
t.tic()       # Start timer
print(f"================================")
print(f'---------------------')
print(f'# Nesting LIST inside DICT:')
programming_language = {
    'Elshad': ['Python', 'Java', 'C#'],
    'Renad': 'Scratch',
    'Edy': 'Java',
}
print(f'programming_language:')
print(json.dumps(programming_language,
                 sort_keys=False,
                 indent=4,
                 separators=(',', ': ')
                 ))


print(f'\n---------------------')
print(f'# Nesting DICT inside LIST:')
programming_language = [
    {
        'user_name': 'Elshad',
        'favorite_language': ['Python', 'Java', 'C#'],
        'experience': 10,
    },
    {
        'user_name': 'Renad',
        'favorite_language': ['Scratch', 'Python'],
        'experience': 2
    },
]
print(json.dumps(programming_language,
                 sort_keys=False,
                 indent=4,
                 separators=(',', ': ')
                 ))


print(f'\n---------------------')
print(f'# Nesting DICT inside DICT:')
programming_language = {
    'Elshad': {
        'favorite_language': ['Python', 'Java', 'C#'],
        'experience': 10,
    },
    'Renad': {
        'favorite_language': ['Scratch', 'Python'],
        'experience': 2,
    }
}
print(f'programming_language:')
print(json.dumps(programming_language,
                 sort_keys=False,
                 indent=4,
                 separators=(',', ': ')
                 ))
print(f"================================")
t.toc() # Print elapsed time: "Elapsed time is <xxx> seconds."

# RESULT:
"""
# ================================
# ---------------------
# # Nesting LIST inside DICT:
# programming_language:
# {
#     "Elshad": [
#         "Python",
#         "Java",
#         "C#"
#     ],
#     "Renad": "Scratch",
#     "Edy": "Java"
# }

# ---------------------
# # Nesting DICT inside LIST:
# [
#     {
#         "user_name": "Elshad",
#         "favorite_language": [
#             "Python",
#             "Java",
#             "C#"
#         ],
#         "experience": 10
#     },
#     {
#         "user_name": "Renad",
#         "favorite_language": [
#             "Scratch",
#             "Python"
#         ],
#         "experience": 2
#     }
# ]

# ---------------------
# # Nesting DICT inside DICT:
# programming_language:
# {
#     "Elshad": {
#         "favorite_language": [
#             "Python",
#             "Java",
#             "C#"
#         ],
#         "experience": 10
#     },
#     "Renad": {
#         "favorite_language": [
#             "Scratch",
#             "Python"
#         ],
#         "experience": 2
#     }
# }
# ================================
# Elapsed time is 0.003771 seconds.
"""