2

I am producing a list of list by this code, it is just a condition for a column of a dataframe named lvl or "Level", then append the index of this condition values, so the problem that i got is that the order of appending is important to me,

for i in range(1,int(24-lvl)): j=list2[(list2.lvl==(lvl+i))] jj=[] jj.append(j.index) print itertools.chain(jj)

well for example, the answer should be: [0,100,110,500,501,550,555,89,120,114] but i get the same list but sorted [0,89,100,110,114,120,500,501,550,555]

jmparejaz
  • 160
  • 1
  • 14

2 Answers2

3

itertools.chain works for me. You need to unpack the list before passing it to chain method.

>>> l = [[1,5],[10,2],[6,9,3]]
>>> list(itertools.chain(*l))
[1, 5, 10, 2, 6, 9, 3]
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

You can simply do it with list comprehension:

>>> l = [[1,5],[10,2],[6,9,3]]
>>> l_out = [item for sub_l in l for item in sub_l]
>>> l_out
[1, 5, 10, 2, 6, 9, 3]
Iron Fist
  • 10,739
  • 2
  • 18
  • 34