simple way to un-nest (flatten?) list
George Sakkis
george.sakkis at gmail.com
Sun Nov 5 17:25:59 EST 2006
djc wrote:
> There is I am sure an easy way to do this, but I seem to be brain dead
> tonight. So:
>
> I have a table such that I can do
>
> [line for line in table if line[7]=='JDOC']
> and
> [line for line in table if line[7]=='Aslib']
> and
> [line for line in table if line[7]=='ASLIB']
> etc
>
> I also have a dictionary
> r= {'a':('ASLIB','Aslib'),'j':('JDOC', 'jdoc')}
> so I can extract values
> r.values()
> [('ASLIB', 'Aslib'), ('JDOC', 'jdoc')]
>
> I would like to do
>
> [line for line in table if line[7] in ('JDOC','jdoc','Aslib','ASLIB')]
>
> so how should I get from
> {'a':('ASLIB','Aslib'),'j':('JDOC','jdoc')}
> to
> ('Aslib','ASLIB','JDOC','jdoc')
Meet itertools:
from itertools import chain
names = set(chain(*r.itervalues()))
print [line for line in table if line[7] in names]
George
More information about the Python-list
mailing list