Home > Blockchain >  How to assign values of one list to values of second list when their length is not equal?
How to assign values of one list to values of second list when their length is not equal?

Time:06-17

Input is:

B = [1,2,3,4,5,6,7]
A = ['A','B','C']

Output should be:

C = {'A':[1,4,7], 'B':[2,5], 'C':[3,6]}

The length of lists are dynamic (not fixed) but length of list B is always going to be greater than length of list A.

CodePudding user response:

You can do it with a dictionary comprehension and slicing:

{j: B[i::len(A)] for i, j in enumerate(A)}

Output:

{'A': [1, 4, 7], 'B': [2, 5], 'C': [3, 6]}
  • Related