Skip to content

Instantly share code, notes, and snippets.

@FerdinaKusumah
Last active July 29, 2021 05:52
Show Gist options
  • Save FerdinaKusumah/ba9349fe0bc0cce4d4849f5d9b23b615 to your computer and use it in GitHub Desktop.
Save FerdinaKusumah/ba9349fe0bc0cce4d4849f5d9b23b615 to your computer and use it in GitHub Desktop.
Python Namedtuple
from collections import namedtuple
HobbyNameTuple = namedtuple("Hobby", ["name"])
StudentNameTuple = namedtuple("Student", ["name", "age", "hobby"])
if __name__ == "__main__":
h = HobbyNameTuple("swimming")
s = StudentNameTuple("John toer", 18, [h._asdict()])
result = s._asdict()
print(result)
# result is
# {'name': 'John toer', 'age': 18, 'hobby': [{'name': 'swimming'}]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment