I have a Dictionary in python which have some blank values. i want to fill that blank values with numbers consecutively in a row.
Below is my dictionary dataset which have blank values
Dict1 = {"BM":["A","B","C"],"ASM":["D","E","F"],"CSM":[""],"DSM":[""],"RSM":["G","H"],"ZSM":["I"],"NSM":["J","","l"]}
I tried below code
`max_length = max(len(lst) for lst in designation_dict.values())
# Pad the lists in the designation dictionary with 'L{n}' to make them equal length
for key in designation_dict:
for i in range(max_length):
if i < len(designation_dict[key]):
if designation_dict[key][i] == "":
designation_dict[key][i] = f'L{i+1}'
else:
designation_dict[key].append(f'L{i+1}')
`
This code retunrs below output
{'BM': ['A', 'B', 'C'],
'ASM': ['D', 'E', 'F'],
'CSM': ['L1', 'L2', 'L3'],
'DSM': ['L1', 'L2', 'L3'],
'RSM': ['G', 'H', 'L3'],
'ZSM': ['I', 'L2', 'L3'],
'NSM': ['J', 'L2', 'l']}
but my expected output is
{'BM': ['A', 'B', 'C'],
'ASM': ['D', 'E', 'F'],
'CSM': ['L1', 'L1', 'L1'],
'DSM': ['L2', 'L2', 'L2'],
'RSM': ['G', 'H', 'L3'],
'ZSM': ['I', 'L3', 'L4'],
'NSM': ['J', 'L4', 'l']}
New contributor
pritesh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.