For example, convert
d = {'a.b1': [1,2,3], 'a.b2': [3,2,1], 'b.a1': [2,2,2]}
to
l = [['a','b1',1,2,3], ['a','b2',3,2,1], ['b','a1',2,2,2]]
What I do now
l = []
for k,v in d.iteritems():
a = k.split('.')
a.extend(v)
l.append(a)
is definitely not a pythonic way.
Python 2:
d = {'a.b1': [1,2,3], 'a.b2': [3,2,1], 'b.a1': [2,2,2]}
l = [k.split('.') + v for k, v in d.iteritems()]
Python 3:
d = {'a.b1': [1,2,3], 'a.b2': [3,2,1], 'b.a1': [2,2,2]}
l = [k.split('.') + v for k, v in d.items()]
These are called list comprehensions.
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