This question already has an answer here:
Is the a short syntax for joining a list of lists into a single list( or iterator) in python?
For example I have a list as follows and I want to iterate over a,b and c.
x = [["a","b"], ["c"]]
The best I can come up with is as follows.
result = []
[ result.extend(el) for el in x]
for el in result:
print el
What you're describing is known as flattening a list, and with this new knowledge you'll be able to find many solutions to this on Google (there is no built-in flatten method). Here is one of them, from http://www.daniel-lemire.com/blog/archives/2006/05/10/flattening-lists-in-python/:
For one-level flatten, if you care about speed, this is faster than any of the previous answers under all conditions I tried. (That is, if you need the result as a list. If you only need to iterate through it on the fly then the chain example is probably better.) It works by pre-allocating a list of the final size and copying the parts in by slice (which is a lower-level block copy than any of the iterator methods):
Sorted times list with comments:
Late to the party but ...
I'm new to python and come from a lisp background. This is what I came up with (check out the var names for lulz):
Seems to work. Test:
returns:
If you need a list, not a generator, use
list():
Or a recursive operation: