In this tutorial, we will certainly check out just how to combine 2 checklists conversely in Python with the assistance of some instances.
The Trouble Statement


Before we continue allow’s check out what we really indicate by combining 2 checklists conversely.
Let’s state you are provided 2 checklists, list1
and also list2
and also you’re asked to combine both checklists with each other right into a brand-new listing such that it consists of components from both checklists conversely beginning with the very first listing. Not that the checklists require not always be of the exact same size.
As an example,
list1
has the components [a1, a2, a3]
list2
has the components [b1, b2, b3]
After combining, the resulting listing needs to appear like [a1, b1, a2, b2, a3, b3]
.
Let’s check out an additional instance where the checklists are not of the exact same size.
list1
has the components [a1, a2, a3, a4, a5]
list2
has the components [b1, b2, b3]
After combining, the resulting listing needs to appear like [a1, b1, a2, b2, a3, b3, a4, a5]
. You can see that the continuing to be components from the bigger listing were added at the end of the resulting listing.
Exactly how to combine 2 checklists conversely?
We will certainly develop a brand-new listing arising from conversely combining the components of both checklists. You can utilize the complying with actions–
- Create variables to monitor the existing index in both checklists.
- Develop a boolean flag to alternative in between both checklists.
- Develop the resulting listing as a vacant listing.
- Usage a while loophole to repeat via the checklists rotating in between both checklists making use of the flag produced over. The loophole quits if either of the index variables gets to completion.
- Lastly, concatenate the continuing to be components (if the checklists are not of the exact same sizes) to the resulting listing.
Let’s check out the code.
# feature to combine 2 checklists conversely def merge_two_lists_alternatively( ls1, ls2):. # develop variables to monitor indices. i = 0. j = 0. # flag to alternative in between the checklists. flag = Real. # the resulting listing. res = []. # repeat via the listing components conversely. while i < < len( ls1) and also j < < len( ls2):. if flag:. res.append( ls1[i]). i += 1. flag = False. else:. res.append( ls2[j]). j += 1. flag = Real. # add the continuing to be components. if i == len( ls1):. res = res + ls2[j:]. else:. res = res + ls1[i:]. # return the resulting listing. return res
Merge Checklists of the Very same Length
Let's currently utilize the above code to combine 2 checklists of the exact same size conversely.
# both checklists to be combined. ls1 = ['a1', 'a2', 'a3']. ls2 = ['b1', 'b2', 'b3']. # combine checklists conversely. ls = merge_two_lists_alternatively( ls1, ls2). print( ls)
Output:
['a1', 'b1', 'a2', 'b2', 'a3', 'b3']
You can see that the resulting listing consists of alternative components from ls1
and also ls2
.
Merge Checklists of Various Lengths
Let's currently use the above approach to 2 checklists that do not have the exact same size.
# both checklists to be combined. ls1 = ['a1', 'a2', 'a3', 'a4', 'a5']. ls2 = ['b1', 'b2', 'b3']. # combine checklists conversely. ls = merge_two_lists_alternatively( ls1, ls2). print( ls)
Output:
['a1', 'b1', 'a2', 'b2', 'a3', 'b3', 'a4', 'a5']
You can see that the continuing to be components are added at the end.
You may additionally want--
Subscribe to our e-newsletter for even more useful overviews and also tutorials.
We do not spam and also you can pull out whenever.