Home > Mobile >  Merging of lists
Merging of lists

Time:08-25

You have two lists say

ListA =['AB' , 'EF']
ListB = ['CD', 'GH']

Get a third list which will be like this

ListC =['AB', 'CD', 'EF', 'GH']

How to do it in Python?

CodePudding user response:

If you don't want to run a loop with 2 lists, you can always use zip()

ListA =['AB' , 'EF']
ListB = ['CD', 'GH']

ListC= []
for every in zip(ListA, ListB):
    ListC.extend(every)

CodePudding user response:

In python you can concatenate two sequences with .

ListA = ['AB', 'EF']
ListB = ['CD', 'GH']

ListC = ListA   ListB

print(ListC)  # ['AB', 'EF', 'CD', 'GH']

Then you can also sort it to your liking with list.sort

  • Related