-2

l have a list as follow:

My_list=[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [2, 5, 8, 11, 6, 4, 8, 1, 2, 7, 1]]

such that :

a=[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

and

b=[2, 5, 8, 11, 6, 4, 8, 1, 2, 7, 1]

Then l append them as follow

My_list.append(a)
My_list.append(b)

l would like to transform it to :

 My_list=[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,2, 5, 8, 11, 6, 4, 8, 1, 2, 7, 1]

How can l remove a and b brackets ?

Jean-François Fabre
  • 131,796
  • 23
  • 122
  • 195
eric lardon
  • 331
  • 1
  • 4
  • 20

4 Answers4

2

Use itertools

Ex:

from itertools import chain
My_list=[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [2, 5, 8, 11, 6, 4, 8, 1, 2, 7, 1]]
print(list(chain(*My_list)))

Output:

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 5, 8, 11, 6, 4, 8, 1, 2, 7, 1]
Rakesh
  • 78,594
  • 17
  • 67
  • 103
1

Try using extend:

My_list = []

a=[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
b=[2, 5, 8, 11, 6, 4, 8, 1, 2, 7, 1]

My_list.extend(a)
My_list.extend(b)

My_list
#[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,2, 5, 8, 11, 6, 4, 8, 1, 2, 7, 1]

Otherwise you can flatten already created list:

My_list = [i for sublist in My_list for i in sublist]
zipa
  • 26,044
  • 6
  • 38
  • 55
1

Try

My_list = a[:]
My_list.extends(b) 
Abhishek
  • 145
  • 2
  • 13
0
a=[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
b=[2, 5, 8, 11, 6, 4, 8, 1, 2, 7, 1]

Python 2.7

new_list = a + b

Python 3.6

new_list = [*a,*b]
M. Prokhorov
  • 3,697
  • 24
  • 35
Pradam
  • 773
  • 7
  • 16