Shallow, Deep, and Iterators

Hello and welcome to this weeks blog posting. This evening we’re going to discuss three aspects of data manipulation; shallow copy, deep copy, and various iterators utilizing the itertools module.

Shallow copy, is essentially constructing a new object by composition to the extent possible, and then inserts by reference to the object. For example, in the below examples we can see the data set in it’s original state, shallow copied, a child element of one parent properties modified, and lastly the shallow object and original objects printed to the console. Since this function utilizes a shallow copy, thus a reference, the child element was modified in the original data set.

Deep copy, is essentially constructing a new object by composition to the extent possible, and then recursively inserts by coping to the object. For example, in the below examples we can see the data set in it’s original state, deep copied, a child element of one parent properties modified, and lastly the deep object and original objects printed to the console. Since this function utilizes a deep copy, thus a recursive copy, the child element was NOT modified in the original data set.

The next section we’re going to discuss are iterators. Specifically, iterators utilizing the itertools module imported into our program. Below are various examples of iterators; simple implementation using a for loop, an example utilizing a infinite loop with a break after a few iterations, an example utilizing the cycle function within the itertools module, and lastly the repeat function within the itertools module.

Well, that about wraps up this weeks blog post. We discussed shallow copy, deep copy, and various examples of iterators utilizing the itertools module. For full code of these examples please feel free to visit my GitHub page: https://github.com/joshsnyder/python-data-science/tree/develop/module8

Leave a comment

Design a site like this with WordPress.com
Get started