1

Trying to convert below list of dict to dict as below:

mylist=[{'A1': 'AAA', 'B1': '0.0000300'}, {'A1': 'BBB', 'B1': '0.0164500'}, {'A1': 'CCC', 'B1': '0.00179350'}

And below dict output is what I am looking for:

neededdict={'AAA': '0.0000300', 'BBB': '0.0164500', 'CCC': '0.00179350'}

but should not be as below:

notneeded={'A1': 'AAA', 'B1':'0.0000300','A1': 'BBB', 'B1': '0.0164500','A1': 'CCC', 'B1': '0.00179350'}

How to solve this problem ?

J87
  • 179
  • 1
  • 1
  • 9

2 Answers2

1

Try this simple dictionary comprehension:

{i['A1']: i['B1'] for i in mylist}
# {'AAA': '0.0000300', 'BBB': '0.0164500', 'CCC': '0.00179350'}
sacuL
  • 49,704
  • 8
  • 81
  • 106
1

Using dict()

Demo:

mylist=[{'A1': 'AAA', 'B1': '0.0000300'}, {'A1': 'BBB', 'B1': '0.0164500'}, {'A1': 'CCC', 'B1': '0.00179350'}]
print( dict((i["A1"], i["B1"]) for i in mylist))

Output:

{'AAA': '0.0000300', 'BBB': '0.0164500', 'CCC': '0.00179350'}
Rakesh
  • 81,458
  • 17
  • 76
  • 113