avatarNaina Chaturvedi

Summary

The website content provides an in-depth exploration of advanced Python concepts, focusing on the itertools module, and offers a comprehensive list of resources for further learning in data science, machine learning, and system design.

Abstract

The article "Advanced Python Made Easy — Part 4" delves into Python's itertools module, explaining various functions such as combinations, permutations, and product, with code examples to illustrate their usage. It is part of a series aimed at advancing the reader's Python skills. Additionally, the content serves as a hub for tech enthusiasts, offering links to a multitude of educational series and projects on topics ranging from machine learning operations (MLOps) to data engineering, as well as a newsletter for tech interview tips and techniques. The article also promotes a YouTube channel, Ignito, which will feature project and coding exercise videos. Furthermore, it provides a compilation of the author's previous work, including a complete system design series, and concludes with a list of recommended articles and programming humor pieces.

Opinions

  • The author believes in the practical application of Python's itertools module to solve combinatorial problems efficiently.
  • There is an emphasis on the importance of understanding advanced Python concepts for tech professionals.
  • The author values the sharing of knowledge and resources, as evidenced by the extensive list of educational content provided.
  • The inclusion of a tech newsletter and YouTube channel suggests the author's commitment to continuous learning and community engagement.
  • Humor in programming is recognized as a valuable aspect of the coding community, with several links to programming humor articles.
  • The author's perspective on learning is hands-on, encouraging readers to engage with code examples and real-world projects.

Advanced Python Made Easy — Part 4

Use these hacks and techniques…

Pic from Unsplash.com

This is the part 4 of Advanced Python Made Easy. You can refer to the first three parts here —

Some of the other best Series —

30 days of Machine Learning Ops

30 Days of Natural Language Processing ( NLP) Series

30 days of Data Engineering with projects Series

Data Science and Machine Learning Research ( papers) Simplified **

60 days of Data Science and ML Series with projects

100 days : Your Data Science and Machine Learning Degree Series with projects

23 Data Science Techniques You Should Know

Tech Interview Series — Curated List of coding questions

Complete System Design with most popular Questions Series

Complete Data Visualization and Pre-processing Series with projects

Complete Python Series with Projects

Complete Advanced Python Series with Projects

Kaggle Best Notebooks that will teach you the most

Complete Developers Guide to Git

Exceptional Github Repos — Part 1

Exceptional Github Repos — Part 2

All the Data Science and Machine Learning Resources

210 Machine Learning Projects

Tech Newsletter —

If you are interested, you can join my newsletter through which I send tech interview tips, techniques, patterns, hacks — Software Development, ML, Data Science, Startups and Technology projects to more than 30K readers. You can subscribe to Tech Brew :

Github —

Projects Videos —

All the projects, data structures, SQL, algorithms, system design, Data Science and ML , Data Analytics, Data Engineering, , Implemented Data Science and ML projects, Implemented Data Engineering Projects, Implemented Deep Learning Projects, Implemented Machine Learning Ops Projects, Implemented Time Series Analysis and Forecasting Projects, Implemented Applied Machine Learning Projects, Implemented Tensorflow and Keras Projects, Implemented PyTorch Projects, Implemented Scikit Learn Projects, Implemented Big Data Projects, Implemented Cloud Machine Learning Projects, Implemented Neural Networks Projects, Implemented OpenCV Projects,Complete ML Research Papers Summarized, Implemented Data Analytics projects, Implemented Data Visualization Projects, Implemented Data Mining Projects, Implemented Natural Leaning Processing Projects, MLOps and Deep Learning, Applied Machine Learning with Projects Series, PyTorch with Projects Series, Tensorflow and Keras with Projects Series, Scikit Learn Series with Projects, Time Series Analysis and Forecasting with Projects Series, ML System Design Case Studies Series videos will be published on our youtube channel ( just launched).

Subscribe today!

Today in this post we are going to take a deep dive in Python’s Itertools.

Let’s dive in !

Itertools

Python Itertools are fast, memory efficient functions— a collection of constructs for handling iterators. Iterators are nothing but an object that contains a countable number of value i.e values you can traverse through and can be Infinite iterators, Combinatoric iterators, Terminating iterators. List, tuples, dictionaries, and sets are all iterable objects.

To create an object as an iterator you have to implement the methods __iter__() and __next__() to your object,

where —

__iter__() returns the iterator object itself. This is used in for and in statements.

__next__() method returns the next value in the sequence. In order to avoid the iteration to go on forever, raise the StopIteration exception.

Implementation of itertools functions —

i) combinations()

Syntax —

itertools.combinations(iterable, r)

Code —

import itertools
countries = ['USA', 'Australia', 'Canada','Germany']
result = itertools.combinations(countries, 3)
for i in result:
    print(i)

Output —

('USA', 'Australia', 'Canada')
('USA', 'Australia', 'Germany')
('USA', 'Canada', 'Germany')
('Australia', 'Canada', 'Germany')

ii) combinations_with_replacement ()

It allows the element repetition.

Syntax —

itertools.combinations_with_replacement(iterable, r)

Code —

import itertools
countries = ['USA', 'Australia', 'Canada','Germany']
result = itertools.combinations_with_replacement(countries, 3)
for i in result:
    print(i)

Output —

('USA', 'USA', 'USA')
('USA', 'USA', 'Australia')
('USA', 'USA', 'Canada')
('USA', 'USA', 'Germany')
('USA', 'Australia', 'Australia')
('USA', 'Australia', 'Canada')
('USA', 'Australia', 'Germany')
('USA', 'Canada', 'Canada')
('USA', 'Canada', 'Germany')
('USA', 'Germany', 'Germany')
('Australia', 'Australia', 'Australia')
('Australia', 'Australia', 'Canada')
('Australia', 'Australia', 'Germany')
('Australia', 'Canada', 'Canada')
('Australia', 'Canada', 'Germany')
('Australia', 'Germany', 'Germany')
('Canada', 'Canada', 'Canada')
('Canada', 'Canada', 'Germany')
('Canada', 'Germany', 'Germany')
('Germany', 'Germany', 'Germany')

iii) count ()

Syntax —

itertools.count(start, step)

Makes an iterator that returns evenly spaced values starting with number specified with start.

Code —

import itertools
for i in itertools.count(20,4):
    print(i)
    if i > 30:
        break

Output —

20
24
28
32

iv) groupby()

To group things together just like in SQL.

Syntax —

itertools.groupby(iterable, key=None)

Code —

import itertools 
  
countries = [("West", "USA"),  
          ("West", "Canada"),  
          ("East", "Singapore"),  
          ("East", "China")] 
  
iterator_one = itertools.groupby(countries, lambda x : x[0]) 
  
for key, group in iterator_one: 
    result = {key : list(group)} 
    print(result)

Output —

{'West': [('West', 'USA'), ('West', 'Canada')]}
{'East': [('East', 'Singapore'), ('East', 'China')]}

v)permutations()

Syntax —

itertools.permutations(iterable, r=None)

Code —

from itertools import permutations  
name = "Adam"
   
result = permutations(name)  
  
for j in list(result):  
    print(j)

Output —

('A', 'd', 'a', 'm')
('A', 'd', 'm', 'a')
('A', 'a', 'd', 'm')
('A', 'a', 'm', 'd')
('A', 'm', 'd', 'a')
('A', 'm', 'a', 'd')
('d', 'A', 'a', 'm')
('d', 'A', 'm', 'a')
('d', 'a', 'A', 'm')
('d', 'a', 'm', 'A')
('d', 'm', 'A', 'a')
('d', 'm', 'a', 'A')
('a', 'A', 'd', 'm')
('a', 'A', 'm', 'd')
('a', 'd', 'A', 'm')
('a', 'd', 'm', 'A')
('a', 'm', 'A', 'd')
('a', 'm', 'd', 'A')
('m', 'A', 'd', 'a')
('m', 'A', 'a', 'd')
('m', 'd', 'A', 'a')
('m', 'd', 'a', 'A')
('m', 'a', 'A', 'd')
('m', 'a', 'd', 'A')

vi) product()

It gives the Cartesian product of the input iterables.

Syntax —

itertools.product(*iterables, repeat=1)

Code —

from itertools import product 
  
def cart_product(arr1, arr2): 
  
    return list(product(arr1, arr2))  
 
arr1 = [100, 200] 
arr2 = [500, 600] 
print(cart_product(arr1, arr2))

Output —

[(100, 500), (100, 600), (200, 500), (200, 600)]

vii) compress()

Syntax — itertools.compress(data, selectors)

Code —

import itertools 
import operator 
  
countries = ['USA', 'Australia', 'Canada','Germany']
selectors = [False, False, True, True] 
  
result = itertools.compress(countries, selectors) 
  
for i in result: 
    print(i)

Output —

Canada
Germany

viii) starmap()

It is used when an iterable is contained within another iterable and to apply any function where each element of iterable to be considered seperate.

Syntax —

itertools.starmap(function, iterable)

Code —

from itertools import starmap 
  
a =[(1,5), (3, 2), (4, 3)] 
  
new_a = list(starmap(pow, a)) 
  
print(new_a)

Output —

[1, 9, 64]

ix) chain()

It is used for treating consecutive sequences as a single sequence.

Syntax —

itertools.chain(*iterables)

Code —

from itertools import chain 
  
list_one =['Steve', 'Naina', 'Groot']
result = list(chain(list_one)) 
  
result_ci = list(chain.from_iterable(li)) 
  
print("Chain Output :", result) 
  
print("chain.from_iterable Output :", result_ci)

Output —

Chain Output : ['Steve', 'Naina', 'Groot']
chain.from_iterable Output : ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L']

x)zip_longest()

It is used to make an iterator that aggregates elements from each of the iterables.

Syntax —

itertools.zip_longest(*iterables, fillvalue=None)

Code —

from itertools import zip_longest 
  
list_one =[100, 200, 300, 400, 500, 600] 
list_two =[800, 900, 1000] 
z = list(zip_longest(list_one, list_two)) 
print(z)

Output —

[(100, 800), (200, 900), (300, 1000), (400, None), (500, None), (600, None)]

All the Complete System Design Series Parts —

1. System design basics

2. Horizontal and vertical scaling

3. Load balancing and Message queues

4. High level design and low level design, Consistent Hashing, Monolithic and Microservices architecture

5. Caching, Indexing, Proxies

6. Networking, How Browsers work, Content Network Delivery ( CDN)

7. Database Sharding, CAP Theorem, Database schema Design

8. Concurrency, API, Components + OOP + Abstraction

9. Estimation and Planning, Performance

10. Map Reduce, Patterns and Microservices

11. SQL vs NoSQL and Cloud

12. Most Popular System Design Questions

Github —

Thanks for Reading. Keep coding :)

Advanced Python Part 5 coming soon !!

Want to read programmers humor?

Recommended Articles -

Programming
Python
Machine Learning
Data Science
Software Development
Recommended from ReadMedium