198

I have many lists:

['it']
['was']
['annoying']

I want to merge those into a single list:

['it', 'was', 'annoying']
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
user1452759
  • 8,810
  • 15
  • 42
  • 58
  • 19
    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. – BrenBarn Jul 20 '12 at 06:55
  • 3
    @BrenBarn That is exactly what I'm asking. – user1452759 Jul 20 '12 at 07:08
  • 1
    Concatenating individual lists **is** flattening a list of lists. Either the "individual lists" already are in a list, or they're in separate variables that should just be collected into a list (or other sequence) anyway. – Karl Knechtel Sep 06 '22 at 06:16

3 Answers3

235
import itertools
ab = itertools.chain(['it'], ['was'], ['annoying'])
list(ab)

Just another method....

Rakesh
  • 81,458
  • 17
  • 76
  • 113
  • 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. – user1452759 Jul 20 '12 at 07:05
  • 1
    @user1452759 `list(itertools.chain(['it'], ['was'], ['annoying']))` gives `['it', 'was', 'annnoying']`. Is that different from what you want? – lvc Jul 20 '12 at 07:15
  • ... or looking for "list.extend", if already have it wrapped in a list. – ToolmakerSteve Dec 19 '13 at 23:24
  • 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) – yucer Dec 02 '15 at 13:00
  • 29
    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 – Fnord Mar 22 '17 at 18:45
  • 11
    You could also pass it as an `*args` by saying: `itertools.chain(*my_list_of_lists)`. – Oxcug Jul 21 '17 at 19:39
  • Kinda makes `chain.from_iterable` pointless doesn't it? – BallpointBen Jun 10 '18 at 06:10
  • 1
    @BallpointBen No, because `from_iterable` it accepts any iterable, which can be e.g. generator returning subsequent lists. – Michał Góral May 24 '19 at 09:16
185

Just add them:

['it'] + ['was'] + ['annoying']

You should read the Python tutorial to learn basic info like this.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • 226
    You solved the i=3 case, how about i=n? – William Entriken Aug 15 '17 at 02:30
  • @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. – zhihong Sep 29 '17 at 13:54
  • @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. – kittenparry Dec 19 '18 at 01:21
  • I was able to solve the problem for i = n using the "flatten a list of lists" approach in the "Possible duplicate" list – Niema Moshiri Apr 21 '19 at 01:30
  • 3
    @WilliamEntriken , Technically you can use build in sum for example `sum(mylist, [])` but it is not very optimize. – Zen3515 Jul 13 '19 at 04:41
  • For the i=n, you can do this: – j4n7 Oct 04 '19 at 14:46
  • 2
    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. – Sayyor Y Jan 16 '21 at 08:00
64
a = ['it']
b = ['was']
c = ['annoying']

a.extend(b)
a.extend(c)

# a now equals ['it', 'was', 'annoying']
Scotty
  • 2,480
  • 2
  • 16
  • 20
  • 4
    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 ? – diffracteD Jul 20 '16 at 13:28