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

Python Combine two list

I have

A = ['A','B','C','D','E','F','G','H','J']
B= ['a','b','c']

I want to combine the list that ‘a’ is combine with first three element of list A , ‘b’ with the next three and ‘c’ with the last three as shown below

C = ['Aa','Ba','Ca','Db','Eb','Fb','Gc','Hc','Jc,]

how can I go about it in python

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 :

As a list comprehension you could do this. Although I suspect there’s a nicer way to do it.

[f"{capital}{B[i//3]}" for i,capital in enumerate(A)]

i will increment by 1 for each letter in A so we can do floor division by 3 to only increment it every 3 iterations of A giving us the correct index of B and just use an f-string to concaninate the strings although capital + B[i//3] works too.

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