Table of Contents

You must be familiar with the string method lstrip([chars]) in Python's standard library. When this method is applied on a string, it returns a copy of the string with the leading characters (in chars) removed.

>>> a_string = "   ,#!   some string  ,#!   "
>>> a_string.lstrip(" ,#!")
'some string  ,#!   '

In this blog post, we are going to see whether the same functionality could be applied to any iterable.

itertools dropwhile function

The itertools module is a very powerful Python module for writing faster and memory efficient code. In an earlier post, we had seen the groupby function of the itertools module. In this post, we are going to explore the dropwhile function of the itertools module. As it turns out, we could apply the lstrip functionality on an iterable using the dropwhile method.

Let's explore how this can be achieved.

Exploring the dropwhile function

itertools.dropwhile(predicate, iterable)

The dropwhile function takes a predicate and an iterable as arguments. It loops over the iterable and drops elements from it as long as the predicate returns True for that element. The predicate could be any callable.

Below I am going to define two predicates:

def is_falsey(an_object):
    return not bool(an_object)

is_falsey returns True if the passed in object is falsey(0, 0.0, {}, [] etc).

def is_negative(number):
    return number < 0

is_negative return True if the passed in number is negative.

Let's use these predicates in the dropwhile function

>>> from itertools import dropwhile
>>> an_iterable = ['', {}, 0, 0.0, 1, 2, 3, '', {}, 0, 0.0]
>>> dropwhile(is_falsey, an_iterable)
<itertools.dropwhile object at 0x7f3c716d8d48>
>>> list(dropwhile(is_falsey, an_iterable))
[1, 2, 3, '', {}, 0, 0.0]
>>>
>>>
>>> numbers = [-2, -1, 0, 1, 2, -2, -1]
>>> list(dropwhile(is_negative, numbers))
[0, 1, 2, -2, -1]
>>>

The above code illustrates the following:

  • The dropwhile function loops over the iterable and removes an element from the iterable, if the predicate returns True for the element.

  • '', {}, 0, 0.0 are all falsey. is_falsey returns True for all these and dropwhile removes these from the iterable.

  • -2, -1 are negative numbers. is_negative returns True for these numbers and dropwhile removes these from the iterable.

The dropwhile function removes only the leading elements(which satisfies the predicate) from the iterable.

  • the trailing falsey elements ('', {}, 0, 0.0) are not removed from an_iterable
  • the trailing negative numbers (-2, -1) are not removed from numbers

The dropwhile function returns an iterator. We used the list constructor on the returned iterator to get back the list.

Conclusion

In this post, we explored the dropwhile and how it can be used to achieve the lstrip functionality similar to the lstrip method's functionality of the built-in string data-type.

If you have reached this far, it is a good time to check whether the itertools module supports the functionality to strip trailing elements from an iterable. It turns out that itertools module does support this functionality through the takewhile function. I urge you to read its docs here.

That's it, readers, until next time. Happy Coding

Share this post:

Leave a comment

Similar Posts


One Day or Day One, you decide.