LISTS: Extract every other element

Randall Hopper aa8vb at yahoo.com
Thu Dec 16 13:13:41 EST 1999


I want to take a large list:

  [ 1,2,3,4,5,6,7,... ]

and build a list with every other element:

  [ 1,3,5,7,... ]


Is there a faster way than looping over indices?:

      lst2 = [ None ] * len(lst)/2
      for i in xrange( 0, len(lst), 2 ):
        lst2[i/2] = lst[i]

-- 
Randall Hopper
aa8vb at yahoo.com




More information about the Python-list mailing list