I need your help to solve a problem.
I want to convert a dictionary d = {key1:value1, key2:value2} into list= [keys1, keys1, ... (value1 times), k
d = {key1:value1, key2:value2}
list= [keys1, keys1, ... (value1 times), k
d1={4:1,3:2,12:2} ans =[] for key,value in d1.items(): ans.append((str(key),)*value) print(ans) flattened = [] list(flattened.extend(item) for item in ans) print(flattened)
Output:
[('4',), ('3', '3'), ('12', '12')] ['4', '3', '3', '12', '12']