I am struggling with this little thingy. Suppose:
field_name = ['name', 'age', 'sex']
field_values = ['john', '24', 'M', 'jane', '26', 'F']
output something like:
{ 'name': ['john','jane'],
'age': ['24', '26'],
'sex': ['M', 'F']
}
Zipping right now:
dict_sample_fields = dict(zip(field_name, field_value))
#output
{ 'name': 'john',
'age': '24',
'sex': 'M'
}
How do I achieve a cyclic zipping on values?
I can achieve this long way having multi-loops. One-liner would be cool :D.
Quite simple really, you don't even need zip
:
{k: field_values[i::len(field_name)] for i, k in enumerate(field_name)}
# {'name': ['john', 'jane'], 'age': ['24', '26'], 'sex': ['M', 'F']}
Make use of the steps in slicing your list
, and starting with the index of the field_name
will do.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With