simple way to un-nest (flatten?) list

djc anrnsrisnotreqid at blueyonder.co.uk
Wed Nov 8 01:22:03 CET 2006


bearophileHUGS at lycos.com wrote:
> It is simple, but I suggest you to take a look at the speed of that
> part of your code into your program. With this you can see the
> difference:
> 
> from time import clock
> d = dict((i,range(300)) for i in xrange(300))
> 
> t = clock()
> r1 = sum(d.values(), [])
> print clock() - t
> 
> t = clock()
> r2 = []
> for v in d.values(): r2.extend(v)
> print clock() - t

Yes, interesting, and well worth noting

1   for v in d.values(): r1.extend(v)

2   from itertools import chain
     set(chain(*d.itervalues()))

3   set(v for t in d.values() for v in t)

4   sum(d.values(), [])

5   reduce((lambda l,v: l+v), d.values())

  on IBM R60e [CoreDuo 1.6MHz/2GB]
  d = dict((i,range(x)) for i in xrange(x))
  x 	t1	t2 	t3 	  t4 	  t5
300 	0.0  	0.02  	0.04  	  0.31    0.32
500 	0.01 	0.09 	0.1 	  1.67 	  1.69
1000 	0.02 	0.3 	0.4 	 16.17 	 16.15
	0.03 	0.28 	0.42 	 16.37 	 16.31
1500 	0.03 	0.76 	0.94 	 57.05 	 57.13
2000 	0.07 	1.2 	1.66 	136.6 	136.97
2500 	0.11 	2.34 	2.64 	268.44 	268.85

but on the other hand, as the intended application is a small command 
line app where x is unlikely to reach double figures and there are only 
two users, myself included:
d = 
{'a':['ASLIB','Aslib'],'j':['JDOC','jdoc'],'x':['test','alt','3rd'],'y':['single',]}
	0.0 	0.0 	0.0 	0.0 	0.0

And sum(d.values(), []) has the advantage of raising a TypeError in the 
case of a possible mangled input.

{'a':['ASLIB','Aslib'],'j':['JDOC','jdoc'],'x':['test','alt','3rd'],'y':'single'}
  r1
['ASLIB', 'Aslib', 'test', 'alt', '3rd', 'JDOC', 'jdoc', 's', 'i', 'n', 
'g', 'l', 'e']
r2
set(['Aslib', 'JDOC', 'g', '3rd', 'i', 'l', 'n', 'ASLIB', 's', 'test', 
'jdoc', 'alt', 'e'])
r4 = sum(d.values(), [])
TypeError: can only concatenate list (not "str") to list



-- 
djc



More information about the Python-list mailing list