Combining lists into one [duplicate]

I am writing a small script to help out with Japanese kana memorisation. How would I combine the following lists into one? I tried as follows.

a = ["a",   "i",   "u",   "e",   "o"]
k = ["ka",  "ki",  "ku",  "ke",  "ko"]
g = ["ga",  "gi",  "gu",  "ge",  "go"]
s = ["sa",  "shi", "su",  "se",  "so"]
z = ["za",  "ji",  "zu",  "ze",  "zo"]
t = ["ta",  "chi", "tsu", "te",  "to"]
d = ["da",         "du",  "de",  "do"]
n = ["na",  "ni",  "nu",  "ne",  "no"]
h = ["ha",  "hi",  "hu",  "he",  "ho"]
b = ["ba",  "bi",  "bu",  "be",  "bo"]
p = ["pa",  "pi",  "pu",  "pe",  "po"]
m = ["ma",  "mi",  "mu",  "me",  "mo"]
y = ["ya",         "yu",         "yo"]
n = ["n"]

kana = [a, k, g, s, z, t, d, n, h, b, p, m, y, n]

print kana

One way:

kana = a + k + g + s + z + t + d + n + h + b + p + m + y + n

The question is effectively asking how do you flatten that list of lists, which is answered here: join list of lists in python.

You could print out everything by doing something like:

import itertools
print list(itertools.chain(*kana))

My +1 for the explicit for loop with .extend()

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
...
Readability counts.
...
In the face of ambiguity, refuse the temptation to guess.
...

When measured, the plain for loop is faster than the side-effect by the list comprehension.

import itertools
import timeit

def flattenListOfLists(lst):
    result = []
    for sublist in lst:
        result.extend(sublist)
    return result

def flattenListOfLists2(lst):
    result = []
    [result.extend(sublist) for sublist in lst]  # uggly side effect ;)
    return result

def flattenIterTools(lst):
    return list(itertools.chain(*lst))


a = ["a",   "i",   "u",   "e",   "o"]
k = ["ka",  "ki",  "ku",  "ke",  "ko"]
g = ["ga",  "gi",  "gu",  "ge",  "go"]
s = ["sa",  "shi", "su",  "se",  "so"]
z = ["za",  "ji",  "zu",  "ze",  "zo"]
t = ["ta",  "chi", "tsu", "te",  "to"]
d = ["da",         "du",  "de",  "do"]
n = ["na",  "ni",  "nu",  "ne",  "no"]
h = ["ha",  "hi",  "hu",  "he",  "ho"]
b = ["ba",  "bi",  "bu",  "be",  "bo"]
p = ["pa",  "pi",  "pu",  "pe",  "po"]
m = ["ma",  "mi",  "mu",  "me",  "mo"]
y = ["ya",         "yu",         "yo"]
n = ["n"]

kana = [a, k, g, s, z, t, d, n, h, b, p, m, y, n]

t = timeit.timeit('lst = flattenListOfLists(kana)', 'from __main__ import kana, flattenListOfLists', number=100000)
print 'for loop:', t

t = timeit.timeit('lst = flattenListOfLists2(kana)', 'from __main__ import kana, flattenListOfLists2', number=100000)
print 'list comprehension side effect:', t

t = timeit.timeit('lst = flattenIterTools(kana)', 'from __main__ import kana, flattenIterTools\nimport itertools', number=100000)
print 'itertools:', t

It prints on my console:

for loop: 0.389831948464
list comprehension side effect: 0.468136159616
itertools: 0.620626692887

Anyway, the time is for repeating the same 100 thousands times. The readability counts is my argument.


kana = sum([a, k, g, s, z, t, d, n, h, b, p, m, y, n], [])

One should also be aware of one very important fact, that the flattened list shares the original objects with the original list of lists. This is not a problem in this case, as the objects are immutable strings. If the objects were mutable, changing them in one structure would change the element value observable via the second structure.

To summarize, one have to know a bit more about Python internals. Sometimes we want to make a copy of the original sublists, like that:

...
result = []
for sublist in lst:
    result.extend(sublist[:])     # notice the [:] here
...