• Latest
  • Trending
All You Need to Know About the Python Itertools Module

All You Need to Know About the Python Itertools Module

March 9, 2022
Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023
Vice President Dr. Bawumia inaugurates  ICT Hub

Vice President Dr. Bawumia inaugurates ICT Hub

April 2, 2023
Co-Creation Hub’s edtech accelerator puts $15M towards African startups

Co-Creation Hub’s edtech accelerator puts $15M towards African startups

February 20, 2023
Data Leak Hits Thousands of NHS Workers

Data Leak Hits Thousands of NHS Workers

February 20, 2023
EU Cybersecurity Agency Warns Against Chinese APTs

EU Cybersecurity Agency Warns Against Chinese APTs

February 20, 2023
How Your Storage System Will Still Be Viable in 5 Years’ Time?

How Your Storage System Will Still Be Viable in 5 Years’ Time?

February 20, 2023
The Broken Promises From Cybersecurity Vendors

Cloud Infrastructure Used By WIP26 For Espionage Attacks on Telcos

February 20, 2023
Instagram and Facebook to get paid-for verification

Instagram and Facebook to get paid-for verification

February 20, 2023
YouTube CEO Susan Wojcicki steps down after nine years

YouTube CEO Susan Wojcicki steps down after nine years

February 20, 2023
Inaugural AfCFTA Conference on Women and Youth in Trade

Inaugural AfCFTA Conference on Women and Youth in Trade

September 6, 2022
  • Consumer Watch
  • Kids Page
  • Directory
  • Events
  • Reviews
Friday, 17 July, 2026
  • Login
itechnewsonline.com
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion
Subscription
Advertise
No Result
View All Result
itechnewsonline.com
No Result
View All Result

All You Need to Know About the Python Itertools Module

Itertools is a common Python module, but how much do you know about it? Find out more by exploring some of the module's most useful functions.

by ITECHNEWS
March 9, 2022
in Data Science, Leading Stories
0 0
0
All You Need to Know About the Python Itertools Module

Itertools is one of the most useful modules Python provides. Its functions make it a breeze to work with lists and arrays. Whether you need all permutations of the values in an array, or you want to group characters from a string, Itertools can help.

Writing the code for such common cases can be tedious and error-prone. Hence, developers have built libraries to do all this for you. You can use these functions by importing Itertools. In this article, you’ll learn about the Itertools module in Python and its functions.

YOU MAY ALSO LIKE

French Telco Orange Hit by Cyber-Attack

ATC Ghana supports Girls-In-ICT Program

What Is the Itertools Module?

The official Python documentation explains that Itertools contains code for building iterators. This module provides fast and efficient functions to work with lists and arrays.

Before using this module, you need to import it using the following syntax:

import itertools

There are three different types of iterators present in this module.

  1. Infinite iterators
  2. Combinatoric iterators
  3. Terminating iterators

Infinite Iterators

Infinite iterators can run a loop infinitely. These functions more often run using a for loop. There are three infinite iterators.

1. count(start, step)

The count() function takes two parameters: the start and the step. The loop begins from the start value and returns values that increment by step, which defaults to 1. Consider the example given below: the loop starts from 2 and will add 2 each time. The loop breaks when the value of i becomes 10.

for i in itertools.count(2,2):
  if i == 10:
      break
  else:
      print(i, end=" ")

Output:

2  4  6  8  

2. repeat(number, timesToRepeat)

The repeat() function accepts two parameters. The first is a value that the function produces repeatedly. The second parameter is the number of times the number should repeat. If you don’t specify the second parameter, the loop will run infinitely.

for i in itertools.repeat(2,5):
   print(i, end=" ")

Output:

2 2 2 2 2 

3. cycle(input)

The cycle() function iterates through the input and prints individual items in a given order. When it reaches the end of its input, cycle restarts from the beginning.

c = 0
var = "12345"
for i in itertools.cycle(var):
  if c == 12:
      break
  else:
      c = c + 1
      print(i, end=" ")

Output:

1 2 3 4 5 1 2 3 4 5 1 2

Combinatoric iterators

The combinatoric iterators provide functions to perform permutations, combinations, and cartesian products.

1. product(input)

The product() function computes the cartesian product of the specified input. It has a repeat parameter that computes the cartesian product of an iterator with itself. It is an optional parameter.

array = [1,2,3]
print(list(itertools.product(array, repeat=2)))
string = "ABC"
print(list(itertools.product(string, "XYZ")))

Output:

Example 1 [(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
Example 2 [('A', 'X'), ('A', 'Y'), ('A', 'Z'), ('B', 'X'), ('B', 'Y'), ('B', 'Z'), ('C', 'X'), ('C', 'Y'), ('C', 'Z')]

2. permutations(input, size)

This function returns a tuple of all the permutations of the given iterable. It accepts two parameters: the iterable and group size. If the group size is not specified, it will form groups of the same length as the iterable itself.

arr = [1,2,3,4]
print(list(itertools.permutations(arr, 2)))

Output:

[(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3)]

 

3. combinations(input, length)

The combinations() function helps to compute the combinations of the given iterator. Note that this function maintains the item order of its input. While permutations includes values that differ only by order, combinations produces unique values.

arr = [1,2,3,4]
print(list(itertools.combinations(arr, 3)))

Output:

[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]

Terminating iterators

Terminating iterators produce output based on the conditions given to the input. You can understand it best from some example functions.

1. accumulate(input, operator)

The accumulate() function accepts two arguments: the iterable and an operator. It produces output by applying the operator to a cumulative total and each input element in turn. The operator is an optional argument. If you don’t pass it, this function will perform addition.

import operator
arr = [1,2,3,4]
print(list(itertools.accumulate(arr)))
print(list(itertools.accumulate(arr, operator.sub)))

Output:

[1, 3, 6, 10]
[1, -1, -4, -8]

2. starmap(function, input)

The starmap() function accepts a function and tuple list as its arguments. It computes return values by applying the function to each tuple in the input. In the example given, this function will compute the maximum value of each tuple and return it in an array.

arr = [(1,2,3), (4,5,6), (7,8,9)]
print(list(itertools.starmap(max, arr)))

Output:

[3, 6, 9]

3. filterfalse(function)

The filterfalse() function returns values that don’t meet the condition in the passed function. The code given below removes all the odd values.

arr = [1,2,3,4]
print(list(itertools.filterfalse(lambda x : x % 2 != 0, arr))) 

Output:

[2, 4]

Continue Your Python Coding Journey

Now that you have learned about the Itertools module, it’s time to explore other Python modules. Python has modules and libraries for a wide set of tasks. They include advanced concepts like Fourier transformation and AI/ML applications.

Source: UNNATI BAMANIA
Via: makeuseof
Tags: Python Itertools Module
ShareTweet

Get real time update about this post categories directly on your device, subscribe now.

Unsubscribe

Search

No Result
View All Result

Recent News

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025
ATC Ghana supports Girls-In-ICT Program

ATC Ghana supports Girls-In-ICT Program

April 25, 2023

About What We Do

itechnewsonline.com

We bring you the best Premium Tech News.

Recent News With Image

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa

July 29, 2025
French Telco Orange Hit by Cyber-Attack

French Telco Orange Hit by Cyber-Attack

July 29, 2025

Recent News

  • Absa and Visa Extend Strategic Partnership to Advance Growth and Innovation Across Africa July 29, 2025
  • French Telco Orange Hit by Cyber-Attack July 29, 2025
  • ATC Ghana supports Girls-In-ICT Program April 25, 2023
  • Vice President Dr. Bawumia inaugurates ICT Hub April 2, 2023
  • Home
  • InfoSec
  • Opinion
  • Africa Tech
  • Data Storage

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In

Add New Playlist

No Result
View All Result
  • Home
  • Tech
  • Africa Tech
  • InfoSEC
  • Data Science
  • Data Storage
  • Business
  • Opinion

© Copyright 2026, All Rights Reserved | iTechNewsOnline.Com - Powered by BackUPDataSystems

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?
Go to mobile version