divided key value from list into two value for dictionary

Want to divided key ‘b’ value from [‘2′,’3’] into ‘b1′:’2’,‘b2′:’3’ for each set of dictionary

Original Coding:

x = [{'a': '1','b': ['2', '3'], 'c': '4'},{'a': '5','b': ['6', '7'],'c': '8'},{'a': '9','b': ['10', '11'],'c': '12'}]

print(x)

[{'a': '1', 'b': ['2', '3'], 'c': '4'},
{'a': '5', 'b': ['6', '7'], 'c': '8'},
{'a': '9', 'b': ['10', '11'], 'c': '12'}]

Expected Result

x = [{'a': '1','b1': '2','b2': '3', 'c': '4'},{'a': '5','b1': '6','b2': '7','c': '8'},{'a': '9','b1': '10','b2': '11','c': '12'}]

print(x)

[{'a': '1', 'b1': '2', 'b2': '3', 'c': '4'},
{'a': '5', 'b1': '6', 'b2': '7', 'c': '8'},
{'a': '9', 'b1': '10', 'b2': '11', 'c': '12'}]

Answer

try update with zip

[f'b{i}' for i in range(1, len(i['b'])+1)] will create b1, b2 so on depending on the length of the list.

x = [{'a': '1','b': ['2', '3'], 'c': '4'},{'a': '5','b': ['6', '7'],'c': '8'},{'a': '9','b': ['10', '11'],'c': '12'}]

for d in x:
    d.update(zip([f'b{i}' for i in range(1, len(d['b'])+1)], d['b']))
    del d['b']

print(x)
[{'a': '1', 'c': '4', 'b1': '2', 'b2': '3'}, {'a': '5', 'c': '8', 'b1': '2', 'b2': '3'}, {'a': '9', 'c': '12', 'b1': '2', 'b2': '3'}]