Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #4071

Re: De-tupleizing a list

From Mark Niemczyk <prahamark@gmail.com>
Newsgroups comp.lang.python
Subject Re: De-tupleizing a list
Date 2011-04-26 12:39 -0700
Organization http://groups.google.com
Message-ID <8975f4de-0109-4767-a1fc-ca610a2dd392@glegroupsg2000goo.googlegroups.com> (permalink)

Show all headers | View raw


Some interesting performance comparisons, under Python 3.2.  Times are relative, and are for an initial list of tuples with 500,000 items.  

(1)    ans = []                                                                 #relative time: 298
         for item in lst:
             ans += list(item)
         return ans

(2)    return [item[0] for item in lst]                          #relative time:  106

(3)    from operator import itemgetter                     #relative time:   84
         return list(map(itemgetter(0), lst))

(4)    import itertools                                                   #relative time:  63
         return list(itertools.chain.from_iterable(lst))

(5)    return [x for (x,) in lst]                                      #relative time:  52


With the caveat that 'your mileage may vary'

Regards,
Mark

Back to comp.lang.python | Previous | NextNext in thread | Find similar


Thread

Re: De-tupleizing a list Mark Niemczyk <prahamark@gmail.com> - 2011-04-26 12:39 -0700
  Re: De-tupleizing a list Chris Angelico <rosuav@gmail.com> - 2011-04-27 05:51 +1000

csiph-web