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']
Share:
396,967

Related videos on Youtube

user1452759
Author by

user1452759

Updated on July 08, 2022

Comments

  • user1452759
    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 Python

    I 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
      BrenBarn almost 12 years
      This 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
      user1452759 almost 12 years
      @BrenBarn That is exactly what I'm asking.
  • user1452759
    user1452759 almost 12 years
    Ok 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
    lvc almost 12 years
    @user1452759 list(itertools.chain(['it'], ['was'], ['annoying'])) gives ['it', 'was', 'annnoying']. Is that different from what you want?
  • ToolmakerSteve
    ToolmakerSteve over 10 years
    ... or looking for "list.extend", if already have it wrapped in a list.
  • yucer
    yucer over 8 years
    maybe 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
    diffracteD almost 8 years
    Yes, 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
    Fnord about 7 years
    If the input is a list of lists, using the from_iterable function such as ab = list(itertools.chain.from_iterable([['it'], ['was'], ['annoying']])) would be give the answer the OP expects
  • Oxcug
    Oxcug almost 7 years
    You could also pass it as an *args by saying: itertools.chain(*my_list_of_lists).
  • William Entriken
    William Entriken over 6 years
    You solved the i=3 case, how about i=n?
  • zhihong
    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
    BallpointBen almost 6 years
    Kinda makes chain.from_iterable pointless doesn't it?
  • kittenparry
    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
    Niema Moshiri about 5 years
    I 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
    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
    Zen3515 almost 5 years
    @WilliamEntriken , Technically you can use build in sum for example sum(mylist, []) but it is not very optimize.
  • j4n7
    j4n7 over 4 years
    For the i=n, you can do this:
  • Sayyor Y
    Sayyor Y over 3 years
    For i=n case, you can also use reduce(lambda l1, l2: l1+l2, lists) where lists is a list or a tuple of the lists.