-2

If I have three lists in python...

letters_1 = ['a','b','c']
letters_2 = ['d','e','f']
letters_3 = ['g','h','i']

How can I combine these to one list like:

['a','b','c','d','e','f','g','h','i']

by using python's built in methods?

Right now I'm using numpy to concatenate lists, but I'd rather use straight Python.

all_letters = np.concatenate((letters_1, letters_2, letters_3))
NewToJS
  • 1,891
  • 4
  • 28
  • 54
  • 1
    Does this answer your question? [How do I concatenate two lists in Python?](https://stackoverflow.com/questions/1720421/how-do-i-concatenate-two-lists-in-python) – Błotosmętek Jun 24 '20 at 17:52

6 Answers6

2

You can do this by adding them together:

letters_1 = ['a','b','c']
letters_2 = ['d','e','f']
letters_3 = ['g','h','i']

letters = letters_1 + letters_2 + letters_3 
print(letters)
JaniniRami
  • 448
  • 3
  • 11
0

You can simply put + between them:

>>> letters_1 + letters_2 + letters_3 
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
Vicrobot
  • 3,516
  • 1
  • 14
  • 29
0

I assume the python standard library should be fine? Then you'd use itertools, something like chain will work then.

all_letters = list(chain(letters_1, letters_2, letters_3))

Lagerbaer
  • 7,272
  • 1
  • 20
  • 37
0

How about:

all_letters = letters_1 + letters_2 + letters_3
print(all_letters)
Michael Hawkins
  • 2,686
  • 18
  • 30
0

you can just use + to join them together as shown in other comments.You need to know the basics and here is a basic tutorial for python that can help you

maria_g
  • 130
  • 6
0
letters_1 = ['a','b','c']
letters_2 = ['d','e','f']
letters_3 = ['g','h','i']
concatenated_list=letters_1+letters_2+letters_3
print(concatenated_list)

OUTPUT:

 >>> ['a','b','c','d','e','f','g','h','i']

In the above code "+" operator is used to concatenate the 3 lists into a single list.

ANOTHER SOLUTION:

 letters_1 = ['a','b','c']
 letters_2 = ['d','e','f']
 letters_3 = ['g','h','i']
 c=[] #Empty list in which we are going to append the values of list (a) and (b)

 for i in letters_1:
     c.append(i)
 for j in letters_2:
     c.append(j)
 for k in letters_3:
     c.append(k)
 print(c)

OUTPUT:

>>> ['a','b','c','d','e','f','g','h','i']
Code Carbonate
  • 475
  • 4
  • 14