LISTS: Extract every other element
Gerrit Holl
gerrit.holl at pobox.com
Fri Dec 17 05:23:04 EST 1999
Randall Hopper wrote:
> 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,... ]
Use the filter() builtin:
>>> nums = range(10)
>>> print nums
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> def odd(i):
... return i % 2
...
>>> filter(odd, nums)
[1, 3, 5, 7, 9]
>>> print filter.__doc__
filter(function, sequence) -> list
Return a list containing those items of sequence for which function(item)
is true. If function is None, return a list of items that are true.
regards,
Gerrit.
--
"The world is beating a path to our door"
-- Bruce Perens, (Open Sources, 1999 O'Reilly and Associates)
11:21am up 12 min, 16 users, load average: 0.33, 0.29, 0.24
More information about the Python-list
mailing list