42

I have a bunch of lists I want to append to a single list that is sort of the "main" list in a program I'm trying to write. Is there a way to do this in one line of code rather than like 10? I'm a beginner so I have no idea...

For a better picture of my question, what if I had these lists:

x = [1, 2, 3]
y = [4, 5, 6]
z = [7, 8, 9]

And want to append y and z to x. Instead of doing:

x.append(y)
x.append(z)

Is there a way to do this in one line of code? I already tried:

x.append(y, z)

And it wont work.

anon
  • 1,407
  • 3
  • 14
  • 12

7 Answers7

60
x.extend(y+z)

should do what you want

or

x += y+z

or even

x = x+y+z
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
25

You can use sum function with start value (empty list) indicated:

a = sum([x, y, z], [])

This is especially more suitable if you want to append an arbitrary number of lists.

Minjoon Seo
  • 526
  • 7
  • 10
20

Extending my comment

In [1]: x = [1, 2, 3]
In [2]: y = [4, 5, 6]
In [3]: z = [7, 8, 9]
In [4]: from itertools import chain
In [5]: print list(chain(x,y,z))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
night-crawler
  • 1,409
  • 1
  • 26
  • 39
  • Simple form of the itertools option which I found useful: `itertools.chain([1, 2, 3], [4, 5, 6], [7, 8, 9])` = `[1, 2, 3, 4, 5, 6, 7, 8, 9]` – NuclearPeon Oct 19 '17 at 23:09
3

To exactly replicate the effect of append, try the following function, simple and effective:

a=[]
def concats (lists):
    for i in lists:
        a==a.append(i)


concats ([x,y,z])
print(a)
jillm_5
  • 171
  • 1
  • 6
2

If you prefer a slightly more functional approach, you could try:

import functools as f

x = [1, 2, 3]
y = [4, 5, 6]
z = [7, 8, 9]

x = f.reduce(lambda x, y: x+y, [y, z], x)

This will enable you to concatenate any number of lists onto list x.

If you would just like to concatenate any number of lists together (i.e. not onto some base list), you can simplify to:

import functools as f
from operator import add
big_list = f.reduce(add, list_of_lists)

Take note that our BFDL has his reservations with regard to lambdas, reduce, and friends: https://www.artima.com/weblogs/viewpost.jsp?thread=98196

To complete this answer, you can read more about reduce in the documentation: https://docs.python.org/3/library/functools.html#functools.reduce

I quote: "Apply function of two arguments cumulatively to the items of sequence, from left to right, so as to reduce the sequence to a single value."

P.S. https://stackoverflow.com/a/33277438/532513 compares the performance of different approaches for list concatenation.

Charl Botha
  • 4,373
  • 34
  • 53
  • Please edit/fix the following: "it does seem to work with lists, and is really fast". The linked answer shows the opposite actually, at 1.5 ms it is the, by far, slowest solution. – Håkon T. Jul 06 '22 at 08:18
  • 1
    I think that might be due to Py 3.7 not being out yet when I wrote that answer. Anyawys, thanks for heads-up, I'll update answer. – Charl Botha Jul 07 '22 at 09:34
1

equivalent to above answer, but sufficiently different to be worth a mention:

my_map = {
   'foo': ['a', 1, 2],
   'bar': ['b', '2', 'c'],
   'baz': ['d', 'e', 'f'],
} 
list(itertools.chain(*my_map.values()))
['d', 'e', 'f', 'a', 1, 2, 'b', '2', 'c']

in the above expression, * is important for groking result as args to chain, this is same as prior chain(x,y,z). Also, note the result is hash-ordered.

msudder
  • 505
  • 3
  • 14
0

In one line , it can be done in following ways

x.extend(y+z)

or

x=x+y+z
wingman__7
  • 679
  • 13
  • 17