What is the pythonic path to this dict to list the conversion?

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 am i doing now

l = []
for k,v in d.iteritems():
  a = k.split('.')
  a.extend(v)
  l.append(a)

definitely not a pythonic way.

+4
source share
2 answers

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()]

They are called a list of concepts .

+8
source

You can do it:

>>> d = {'a.b1': [1,2,3], 'a.b2': [3,2,1], 'b.a1': [2,2,2]}
>>> print([k.split(".") + v for k, v in d.items()])
[['b', 'a1', 2, 2, 2], ['a', 'b1', 1, 2, 3], ['a', 'b2', 3, 2, 1]]
+5
source

Source: https://habr.com/ru/post/1629926/