I have a list labelled Data which I want to order in the following way:
Key order = 'ID', 'GE','FN','LN','MN','EM', 'ST'
Data = ['ID:123 GE:m FN:Amir LN:Maleki EM:[email protected] MN:0400101010 ST:VIC']
I managed to order it while it is in a dictionary like this:
d= {'ID':123, 'GE':'m', 'FN':'Amir', 'LN':'Maleki', 'MN':'0400101010', 'EM':'[email protected]', 'ST':'VIC'}
keyorder = ['ID', 'GE','FN','LN','MN','EM', 'ST']
final = sorted(d.items(), key=lambda i:keyorder.index(i[0]))
print(final)
[('ID', 123), ('GE', 'm'), ('FN', 'Amir'), ('LN', 'Maleki'), ('MN', '0400101010'), ('EM', '[email protected]'), ('ST', 'VIC')]
but can I do this without making the list into a dict. If not how do i turn the list to a dict? Thank you!!