473,606 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Fate of itertools.dropw hile() and itertools.takew hile()

I'm considering deprecating these two functions and would like some
feedback from the community or from people who have a background in
functional programming.

* I'm concerned that use cases for the two functions are uncommon and
can obscure code rather than clarify it.

* I originally added them to itertools because they were found in
other functional languages and because it seemed like they would serve
basic building blocks in combination with other itertools allow
construction of a variety of powerful, high-speed iterators. The
latter may have been a false hope -- to date, I've not seen good
recipes that depend on either function.

* If an always true or always false predicate is given, it can be hard
to break-out of the function once it is running.

* Both functions seem simple and basic until you try to explain them
to someone else. Likewise, when reading code containing dropwhile(),
I don't think it is self-evident that dropwhile() may have a lengthy
start-up time.

* Since itertools are meant to be combined together, the whole module
becomes easier to use if there are fewer tools to choose from.

These thoughts reflect my own experience with the itertools module.
It may be that your experience with them has been different. Please
let me know what you think.

Raymond
Dec 29 '07 #1
17 7667
On Dec 29, 6:10 pm, Raymond Hettinger <pyt...@rcn.com wrote:
These thoughts reflect my own experience with the itertools module.
It may be that your experience with them has been different. Please
let me know what you think.
first off, the itertools module is amazing, thanks for creating it. It
changed the way I think about programming. In fact nowadays I start
all my programs with:

from itertools import *

which may not be the best form, but I got tired of importing every
single function individually or writing out the module name.

Now I never needed the dropwhile() and takewhile() functions, but that
may not mean much. For quite a while I never needed the repeat()
function either. It even looked nonsensical to have an iterator that
simply repeats the same thing over and over. One day I had to solve a
problem that needed repeat() and made me really understand what it was
for and got to marvel at a just how neat the solution was.

i.
Dec 30 '07 #2
On Sat, 29 Dec 2007 15:10:24 -0800, Raymond Hettinger wrote:
* Both functions seem simple and basic until you try to explain them to
someone else.
Oh I don't know about that. The doc strings seem to do an admirable job
to me. Compared to groupby(), the functions are simplicity themselves.

Likewise, when reading code containing dropwhile(), I
don't think it is self-evident that dropwhile() may have a lengthy
start-up time.
*scratches head in confusion*

It isn't? I can understand somebody *under*estimati ng the start-up time
(perhaps because they overestimate how quickly dropwhile() can iterate
through the items). But surely it is self-evident that a function which
drops items has to drop the items before it can start returning?

* Since itertools are meant to be combined together, the whole module
becomes easier to use if there are fewer tools to choose from.
True, but on the other hand a toolbox with too few tools is harder to use
than one with too many tools.

--
Steven
Dec 30 '07 #3
Almost every day I write code that uses itertools, so I find it very
useful, and its functions fast.
Removing useless things and keeping things tidy is often positive. But
I can't tell you what to remove. Here are my usages (every sub-list is
sorted by inverted frequency usage):

I use often or very often:
groupby( iterable[, key])
imap( function, *iterables)
izip( *iterables)
ifilter( predicate, iterable)
islice( iterable, [start,] stop [, step])

I use once in while:
cycle( iterable)
chain( *iterables)
count( [n])
repeat( object[, times])

I have used probably one time or few times:
starmap( function, iterable)
tee( iterable[, n=2])
ifilterfalse( predicate, iterable)

Never used so far:
dropwhile( predicate, iterable)
takewhile( predicate, iterable)

Bye,
bearophile
Dec 30 '07 #4
On Dec 30, 12:10 am, Raymond Hettinger <pyt...@rcn.com wrote:
I'm considering deprecating these two functions and would like some
feedback from the community or from people who have a background in
functional programming.

I am with Steven D'Aprano when he says that takewhile and dropwhile
are clear enough. On the other hand, in my code
base I have exactly zero occurrences of takewhile and
dropwhile, even if I tend to use the itertools quite
often. That should be telling. If my situations is
common, that means that takewhile and dropwhile are
useless in practice and should be deprecated.
But I will wait for other respondents. It may just be
that I never needed them. I presume you did scans of
large code bases and you did not find occurrences of
takewhile and dropwhile, right?
Michele Simionato
Dec 30 '07 #5
On Sat, 29 Dec 2007 15:10:24 -0800, Raymond Hettinger wrote:
These thoughts reflect my own experience with the itertools module.
It may be that your experience with them has been different. Please
let me know what you think.
I seem to be in a minority here as I use both functions from time to time.
One "recipe" is extracting blocks from text files that are delimited by a
special start and end line.

def iter_block(line s, start_marker, end_marker):
return takewhile(lambd a x: not x.startswith(en d_marker),
dropwhile(lambd a x: not x.startswith(st art_marker),
lines))

Maybe these functions usually don't turn up in code that can be called
"recipes" so often but are useful for themselves.

Ciao,
Marc 'BlackJack' Rintsch
Dec 30 '07 #6
On Dec 30, 3:29 am, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
One "recipe" is extracting blocks from text files that are delimited by a
special start and end line.
Neat solution!

I actually need such functionality every once in a while.

Takewhile + dropwhile to the rescue!

i.
Dec 30 '07 #7
On Dec 30, 4:12 pm, Istvan Albert <istvan.alb...@ gmail.comwrote:
On Dec 30, 3:29 am, Marc 'BlackJack' Rintsch <bj_...@gmx.net wrote:
One "recipe" is extracting blocks from text files that are delimited by a
special start and end line.

Neat solution!

I actually need such functionality every once in a while.

Takewhile + dropwhile to the rescue!

i.
On at least one thread and a recipe for this task (http://
aspn.activestat e.com/ASPN/Cookbook/Python/Recipe/521877), the proposed
solutions involved groupby() with an appropriate key function. The
takewhile/dropwhile solution seems shorter and (maybe) easier to read
but perhaps not as flexible and general. Regardless, it's a good
example of takewhile/dropwhile.

George
Dec 30 '07 #8
[bearophile]
Here are my usages (every sub-list is
sorted by inverted frequency usage):

I use often or very often:
groupby( iterable[, key])
imap( function, *iterables)
izip( *iterables)
ifilter( predicate, iterable)
islice( iterable, [start,] stop [, step])

I use once in while:
cycle( iterable)
chain( *iterables)
count( [n])
repeat( object[, times])

I have used probably one time or few times:
starmap( function, iterable)
tee( iterable[, n=2])
ifilterfalse( predicate, iterable)

Never used so far:
dropwhile( predicate, iterable)
takewhile( predicate, iterable)
Thank you for the useful and informative response.
Raymond
Dec 31 '07 #9
[Michele Simionato]
in my code
base I have exactly zero occurrences of takewhile and
dropwhile, even if I tend to use the itertools quite
often. That should be telling.
Thanks for the additional empirical evidence.
I presume you did scans of
large code bases and you did not find occurrences of
takewhile and dropwhile, right?
Yes.
Raymond
Dec 31 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1271
by: anton muhin | last post by:
Hello, everybody! Trying to solve the problem in the subj, I found that I miss some iterator-related tools. Mostly consequental application of the same function to some argument (if I'm not missing something it has a name y-combinator). If we had one, generating the sequence of digits is easy: iter(y(lambda (q, _): divmod(q, n), (x, 0)).next, (0, 0))
18
2615
by: Ville Vainio | last post by:
For quick-and-dirty stuff, it's often convenient to flatten a sequence (which perl does, surprise surprise, by default): ]]] -> One such implementation is at http://aspn.activestate.com/ASPN/Mail/Message/python-tutor/2302348
21
2154
by: Steven Bethard | last post by:
Jack Diederich wrote: > > itertools to iter transition, huh? I slipped that one in, I mentioned > it to Raymond at PyCon and he didn't flinch. It would be nice not to > have to sprinkle 'import itertools as it' in code. iter could also > become a type wrapper instead of a function, so an iter instance could > be a wrapper that figures out whether to call .next or __getitem__ > depending on it's argument. > for item in...
28
2275
by: John Bailo | last post by:
Funny, how Bill Gate uses the Deutsches Bank and Barron's to defraud people and try to wreck his competitors ( he can't ). For example, http://www.reuters.com/financeNewsArticle.jhtml?type=hotStocksNews&storyID=4262964 Oh, but see how Barron's and MS are in bed together...
41
2648
by: rurpy | last post by:
The code below should be pretty self-explanatory. I want to read two files in parallel, so that I can print corresponding lines from each, side by side. itertools.izip() seems the obvious way to do this. izip() will stop interating when it reaches the end of the shortest file. I don't know how to tell which file was exhausted so I just try printing them both. The exhausted one will generate a
8
1693
by: Paul Rubin | last post by:
I just had to write some programs that crunched a lot of large files, both text and binary. As I use iterators more I find myself wishing for some maybe-obvious enhancements: 1. File iterator for blocks of chars: f = open('foo') for block in f.iterchars(n=1024): ... iterates through 1024-character blocks from the file. The default iterator
3
1540
by: Rajanikanth Jammalamadaka | last post by:
>>list(itertools.dropwhile(lambda x: x<5,range(10))) Why doesn't this work? Thanks, Raj
0
1132
by: Fredrik Lundh | last post by:
Rajanikanth Jammalamadaka wrote: it works exactly as specified: Help on class dropwhile in module itertools: class dropwhile(__builtin__.object) | dropwhile(predicate, iterable) --dropwhile object |
0
7938
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8430
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8427
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8298
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5452
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3914
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3962
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2441
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1279
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.