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 get the expected output in list format

below are 2 lst1 and lst2 and expected output is in output as below.

lst1 = ['q','r','s','t','u','v','w','x','y','z']


lst2 =['1','2','3']

Output expected

 [['q','1'], ['r','2'], ['s','3'], ['t','1'],['u','2'],['v','3'],['w','1'],['x','2'],['y','3'], 
['z','1']]"

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 :

This is a very simple approach to this problem.

lst1 = ['q','r','s','t','u','v','w','x','y','z']
lst2 = ['1','2','3']

new_list = []

for x in range(len(lst1)):
    new_list.append([lst1[x], lst2[x % 3]])

print(new_list) # [['q', '1'], ['r', '2'], ['s', '3'], ['t', '1'], ['u', '2'], ['v', '3'], ['w', '1'], ['x', '2'], ['y', '3'], ['z', '1']]

You could also use list comprehension in this case, like so:-

new_list = [[lst1[x], lst2[x % 3]] for x in range(len(lst1))]
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