How to merge multiple lists into one list in python?
396,967
Solution 1
import itertools
ab = itertools.chain(['it'], ['was'], ['annoying'])
list(ab)
Just another method....
Solution 2
Just add them:
['it'] + ['was'] + ['annoying']
You should read the Python tutorial to learn basic info like this.
Solution 3
a = ['it']
b = ['was']
c = ['annoying']
a.extend(b)
a.extend(c)
# a now equals ['it', 'was', 'annoying']
Related videos on Youtube
Author by
user1452759
Updated on July 08, 2022Comments
-
user1452759 almost 2 years
Possible Duplicate:
Making a flat list out of list of lists in Python
Join a list of lists together into one list in PythonI have many lists which looks like
['it'] ['was'] ['annoying']
I want the above to look like
['it', 'was', 'annoying']
How do I achieve that?
-
BrenBarn almost 12 yearsThis isn't really a duplicate of that. That question is asking how to flatten a list of nested lists. This question is much more basic and is just asking how to concatenate individual lists.
-
user1452759 almost 12 years@BrenBarn That is exactly what I'm asking.
-
-
user1452759 almost 12 yearsOk there is a file which has different words in 'em. I have done s = [word] to put each word of the file in list. But it creates separate lists (print s returns ['it]']['was']['annoying']) as I mentioned above. I want to merge all of them in one list.
-
lvc almost 12 years@user1452759
list(itertools.chain(['it'], ['was'], ['annoying']))
gives['it', 'was', 'annnoying']
. Is that different from what you want? -
ToolmakerSteve over 10 years... or looking for "list.extend", if already have it wrapped in a list.
-
yucer over 8 yearsmaybe he was trying to pass a list: L=[['it'], ['was'], ['annoying']] and call itertools.chain(L). That doesn't work. In order to use this solution properly he should use: itertools.chain(*L)
-
diffracteD almost 8 yearsYes, But if you are not sure about the length of the list (which is to be used to extend the main list) then how am I going to extend ? Is it anyway possible to use slicing method to deal with variable length lists ?
-
Fnord about 7 yearsIf the input is a list of lists, using the
from_iterable
function such asab = list(itertools.chain.from_iterable([['it'], ['was'], ['annoying']]))
would be give the answer the OP expects -
Oxcug almost 7 yearsYou could also pass it as an
*args
by saying:itertools.chain(*my_list_of_lists)
. -
William Entriken over 6 yearsYou solved the i=3 case, how about i=n?
-
zhihong over 6 years@Full Decent, thanks for point this out, did not think about that i=n case. But I think if it is i=n case, can loop in the lists and concatenate them. For me, just need the most comma way to join 2 lists without duplication elements.
-
BallpointBen almost 6 yearsKinda makes
chain.from_iterable
pointless doesn't it? -
kittenparry over 5 years@zhihong Hey there, it's been well over a full year but you can use something like
mylist = list(set(mylist))
after merging lists to get rid of the duplicates. -
Niema Moshiri about 5 yearsI was able to solve the problem for i = n using the "flatten a list of lists" approach in the "Possible duplicate" list
-
Michał Góral almost 5 years@BallpointBen No, because
from_iterable
it accepts any iterable, which can be e.g. generator returning subsequent lists. -
Zen3515 almost 5 years@WilliamEntriken , Technically you can use build in sum for example
sum(mylist, [])
but it is not very optimize. -
j4n7 over 4 yearsFor the i=n, you can do this:
-
Sayyor Y over 3 yearsFor i=n case, you can also use
reduce(lambda l1, l2: l1+l2, lists)
wherelists
is a list or a tuple of the lists.