0

I have three items in a list of lists:

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

I want it to look like this:

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

what is the best way to do this in python?

Thanks for the help

English Grad
  • 1,365
  • 5
  • 21
  • 40

5 Answers5

4
>>> test = [[1,2,3], [4,5,6], [7,8,9]]
>>> test[0].extend(test.pop(1))  # OR  test[0] += test.pop(1)
>>> test
[[1, 2, 3, 4, 5, 6], [7, 8, 9]]
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Can you use pop for a range? say if I wanted to combine all 3 could I write: test[0].extend(test.pop(1:2)) – English Grad Jul 04 '14 at 13:56
  • @EnglishGrad, No, `list.pop` does not accept a range. – falsetru Jul 04 '14 at 13:59
  • So what would I do if I wanted to put the 3 lists together? just use the + like stated below? – English Grad Jul 04 '14 at 14:00
  • @EnglishGrad, To get this: `[[1, 2, 3, 4, 5, 6, 7, 8, 9]]` ? – falsetru Jul 04 '14 at 14:01
  • @EnglishGrad, Using list comprehension: `[[x for xs in test for x in xs]]`. If you want a flat list: `[x for xs in test for x in xs]` – falsetru Jul 04 '14 at 14:01
  • @EnglishGrad, See [list comprehension](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions). BTW, list comprehension does not change the list inplace, but returns new list unlike the solution given in my answer. – falsetru Jul 04 '14 at 14:03
  • @EnglishGrad: to get `[[1, 2, 3, 4, 5, 6, 7, 8, 9]]`, use reduce: `[reduce(lambda x, y: x + y, test)]` – njzk2 Jul 04 '14 at 14:09
  • @njzk2, `[sum(test, [])]` is shorter. But I don't recommend them because both are not efficient; they create n or n-1 temporary lists. – falsetru Jul 04 '14 at 14:15
  • I see to be getting lists of lists still when I combine them. How do I go from [[1, 2, 3, 4, 5, 6, 7, 8, 9]] to [1, 2, 3, 4, 5, 6, 7, 8, 9]? – English Grad Jul 04 '14 at 14:20
  • @EnglishGrad, Why don't use simply use this? `[x for xs in test for x in xs]` – falsetru Jul 04 '14 at 14:20
2
test = [test[0] + test[1], test[2]]
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Strikeskids
  • 3,932
  • 13
  • 27
1

If you want to flatten of an arbitrary slice, use a slice assignment and a list comprehension on the part you want to flatten.

This flatten from position n to end of the list:

>>> test = [[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15]]
>>> n=2
>>> test[n:]=[[i for s in test[n:] for i in s]]
>>> test
[[1, 2, 3], [4, 5, 6], [7, 8, 9, 10, 11, 12, 13, 14, 15]]

This flattens up to n (but including n):

>>> test = [[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15]]
>>> test[0:n]=[[i for s in test[0:n] for i in s]]
>>> test
[[1, 2, 3, 4, 5, 6], [7, 8, 9], [10, 11, 12], [13, 14, 15]]

This flattens in the middle (from and including n to include the additional groups specified):

>>> test = [[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15]]
>>> test[n:n+2]=[[i for s in test[n:n+2] for i in s]]
>>> test
[[1, 2, 3], [4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15]]

Flatten all the sublists:

>>> test = [[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15]]
>>> n=len(test)
>>> test[0:n]=[[i for s in test[0:n] for i in s]]
>>> test
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]]

Note that in each case the slice on the assignment side is the same as the slice on the list comprehension side. This allows the use of the same slice object for each.

For the last case of flattening the whole list, obviously, you can just do test=[[i for s in test for i in s]] but the fact the logic is consistent allows you to wrap this in a function use a slice object.

dawg
  • 98,345
  • 23
  • 131
  • 206
0

You could combine the first two items in the list of list with the + operator and you should use '' for your strings

test = [['a','b','c'],['e','f','g'],['h','i','j']]
result = [test[0] + test[1], test[2]]
print result

output:
[['a', 'b', 'c', 'e', 'f', 'g'], ['h', 'i', 'j']]
Nobi
  • 1,113
  • 4
  • 23
  • 41
0

Using the more_itertools package:

import more_itertools as mit

list(mit.chunked(mit.flatten(test), 6))
# [[1, 2, 3, 4, 5, 6], [7, 8, 9]]
pylang
  • 40,867
  • 14
  • 129
  • 121