tags:

views:

78

answers:

4

Let's say I have a list somewhere called majorPowers which contain these two lists:

axis=["germany","italy","japan"]
allies=["russia","uk","us"]

I'd like to insert each of the elements of these lists, into a new mega-list. I'm currently doing this:

>>> temp = []
>>> temp = [ww2.append(t) for t in majorPowers]
>>>ww2
[['germany','italy','japan'],['russia','uk','us']]

How do I adjust this to not use the temp and to insert the individual elements into ww2 instead of the lists themselves(axis and allied).

Also, would the new mega-list itself be classed as a comprehensive list, or the process of making it?

EDIT:

Please note I do not want to do:

for a in list1:
    for b in a:
        c.append(b)

@S.Lott. I understand your point. However I'm trying to learn some of the tricks in Python, instead of the standard way I'd usually do things. This is just to open my mind to Python a little more!

+1  A: 
from itertools import chain
ww2 = list(chain.from_iterable(majorPower))
nosklo
-1: unnecessary use of itertools
Alternatively, since there is a finite number of mayor powers, `list(chain(*mayorPowers))`.
Stephan202
what is wrong with sum(majorPowers,[]) instead?
Anurag Uniyal
what's the problem with using itertools?
nosklo
also from_iterable was introduced in 2.6
Anurag Uniyal
when something can be done in simpler way and works on all version, i will choose simple and shorter version
Anurag Uniyal
but I don't see why my answer is wrong - it doesn't deserve downvoting
nosklo
+5  A: 

It is good that you ask this question, because it is bad form to misuse list comprehensions like that. The code you show uses append, not to generate the elements of temp, but because of its side effects. Avoid side effects in list comprehensions!

So, there are a couple of things you can do. First, you can use itertools.chain:

>>> from itertools import chain
>>> list(chain(*mayorPowers))
['germany', 'italy', 'japan', 'russia', 'uk', 'us']

Instead of passing the elements of mayorPowers as individual arguments to chain, you can also use itertools.chain.from_iterable:

>>> list(chain.from_iterable(mayorPowers))
['germany', 'italy', 'japan', 'russia', 'uk', 'us']

Or you can use extend:

>>> ww2 = []
>>> for mp in mayorPowers:
...     ww2.extend(mp)
...
>>> ww2
['germany', 'italy', 'japan', 'russia', 'uk', 'us']

Or sum (I like this one most, I suppose):

>>> sum(mayorPowers, [])
['germany', 'italy', 'japan', 'russia', 'uk', 'us']

Or, to be a little crazy (uses functools.reduce and operator.add),

>>> from functools import reduce
>>> from operator import add
>>> reduce(add, mayorPowers)
['germany', 'italy', 'japan', 'russia', 'uk', 'us']
Stephan202
I don't think you need to import reduce - I think `reduce` and `map` are builtins (at least from 2.5)
thrope
@thrope: In 2.x yes, but not in 3.x. (`reduce` was often abused. And as you can see, in this example too, it is not the preferred method.)
Stephan202
You always provide a thorough, well written answer, Stephan202. Much appreciated! :)
day_trader
A: 

try extend

for t in majorPowers: ww2.extend(t)

or

sum(majorPowers,[])
Anurag Uniyal
A: 

I would use reduce:

from operator import add
ww2 = reduce(add, majorPowers)
thrope
Downvote with no comment?
thrope