I have a dictionary with list values of a different length. I'm needed to get the values that are in the same position associated to the same key in a new dictionary.
Original Dictionay {A: [1,2] , B: [3,4,5]}
New Dictionary: {A: [1,3], B: [2,4], C: [5]}
I tried a for loop and wrapping my values in zip_longest, but am still getting a list index out of range error.
for x in range(1, max([len(v) for v in sub_folder_notebooks.values()])):
notebook_step[x] = zip_longest([elem[x] for elem in sub_folder_notebooks.values()])
CodePudding user response:
I would add a condition when looping through the values of the dict sub_folder_notebooks
to make sure that you can access elem[x]. not the nicest way but should do the trick:
for x in range(0, max([len(v) for v in sub_folder_notebooks.values()])):
notebook_step[x] = zip_longest([elem[x] for elem in sub_folder_notebooks.values() if len(elem)>x])
I start from x=0 because you want to have the first element as well.