Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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]}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading