How to convert a namedtuple into a list of values and preserving the order of properties?
Question:
from collections import namedtuple
Gaga = namedtuple('Gaga', ['id', 'subject', 'recipient'])
g = Gaga(id=1, subject='hello', recipient='Janitor')
I want to be able to obtain this list (which preserves the order of the properties):
[1, 'hello', 'Janitor']
I could create this list myself manually but there must be an easier way.
I tried:
g._asdict().values()
but the properties are not in the order I want.
Answers:
Why not just list
?
>>> list(g)
[1, 'hello', 'Janitor']
from collections import namedtuple
Gaga = namedtuple('Gaga', ['id', 'subject', 'recipient'])
g = Gaga(id=1, subject='hello', recipient='Janitor')
I want to be able to obtain this list (which preserves the order of the properties):
[1, 'hello', 'Janitor']
I could create this list myself manually but there must be an easier way.
I tried:
g._asdict().values()
but the properties are not in the order I want.
Why not just list
?
>>> list(g)
[1, 'hello', 'Janitor']