-2

I am trying to convert a list of lists to a list of dicts, the list is like,

['A', '100000', Timestamp('2017-01-01 00:00:00'), '003']
['B', '100001', Timestamp('2017-02-01 00:00:00'), '004']

I want to convert it to a list of dicts like,

{'name': 'A', 'id': '100000', 'time': Timestamp('2017-01-01 00:00:00'), 'number': '003'}
{'name': 'B', 'id': '100001', 'time': Timestamp('2017-02-01 00:00:00'), 'number': '004'}

I am wondering what's the best way to do it.

daiyue
  • 7,196
  • 25
  • 82
  • 149

1 Answers1

1

Using dict comprehension and zip():

a = ['A', '100000', Timestamp('2017-01-01 00:00:00'), '003']
b = ['B', '100001', Timestamp('2017-02-01 00:00:00'), '004']

all_lists = [a, b]

keys = ['name', 'id', 'time', 'number']

d = {key: val for a_list in all_lists for key, val in zip(keys, a_list)}  # using dict comprehension
Chen A.
  • 10,140
  • 3
  • 42
  • 61