Make your life easier with these itertools when dealing with iterables
“A lie gets halfway around the world before the truth has a chance to get its pants on.” ― Anonymous
Python provides the itertools package which provides convenience functions for many common iterator operations. We have covered count(), cycle() and chain() in the first part of this series, and compress(), dropwhile(), and groupby() in the second part. In this article, we present a few examples of ifilter(), islice(), imap() and izip().
Continue reading “Python itertools – ifilter, islice, imap, izip”
Remove items from a List and more.
“Happiness is a perfume you cannot pour on others without getting some on yourself.” ― Ralph Waldo Emerson
In a previous article, we covered some python list methods related to adding items. Let us now continue the review of more list methods.
Continue reading “Python – More List Methods and Recipes”
Check out this refresher on ways to add items to python lists
“Share your knowledge. It is a way to achieve immortality.” ― Dalai Lama XIV
Continue reading “Python List Methods: Adding Items”
Boost your python expertise with these examples from itertools.
“Never put off till tomorrow what may be done day after tomorrow just as well.” ― Mark Twain
We are continuing with our series on python itertools. In this article, we cover compress, dropwhile, takewhile and groupby.
Continue reading “Python itertools – compress, dropwhile, takewhile, groupby”
A gentle introduction to python itertools
“Laughter is poison to fear.” ― George R.R. Martin, A Game of Thrones
Python provides a module called itertools which, as the name suggests, provides a bunch of conveniences for dealing with iterations and looping. While you could spend your entire python career without ever having to touch this module, trust me when I say your life will be enriched if you at least know about what is available in itertools.
Continue reading “Python itertools – count, cycle and chain”
Use python class special methods to add idiomatic behavior to your class.
“It is the time you have wasted for your rose that makes your rose so important.” ― Antoine de Saint-Exupéry, The Little Prince
The python class system supports a number of special methods which allows class implementations to mimic built-in python objects objects. These special methods are invoked by the python run-time system in a way similar to the built-in classes. For example, the method __len__() can be defined by a user class which will be invoked when the client code attempts to find the length of the object using len(obj).
Continue reading “Python Class Methods – Implementing Ordered Dictionary”
Learn how to declare and use nested classes in python
“There is nothing either good or bad, but thinking makes it so.” ― William Shakespeare, Hamlet
A nested or inner class is contained within another class. This could be for reasons of encapsulation, where the inner class is not useful by itself. Continue reading “Nested or Inner Classes in Python”
Process data using Java 8 Streams and store in Collections including List, Set or Map.
“If we knew what it was we were doing, it would not be called research, would it?”
― Albert Einstein
Java 8 Streams provide a cool facility to combine several operations in a single statement and express it concisely. Once the data is processed, you can collect the data in a List (or another collection). In this article, we present several methods to collect the results of these stream operations into collections.
Continue reading “Convert Java 8 Streams to Collections”
Covers the basic operations on a java TreeSet including creation, iteration, finding elements, etc.
The unexamined life is not worth living.
A java Set is a Collection containing only unique elements. A TreeSet is an implementation of the Set which uses a Red-Black tree implementation. In this article, we demonstrate how to properly create and use a TreeSet.
Continue reading “Using a Java TreeSet”
Learn about several methods to remove items in a loop from a java collection.
“Puns are the highest form of literature.”
― Alfred Hitchcock
When you have a List or a Set or any other Collection, you may sometimes need to remove items in a loop. For example, you may be maintaining a list of messages for processing, and need to occasionally purge stale items from the list. In such a scenario, you would loop around the collection and remove the stale items.
Continue reading “Removing Items in a Loop from a List”