What is itertools.groupby in Python?

Itertools is a module in Python that enables fast, and memory-efficient iteration over iterable data structures.

The module has a number of functions that construct and return iterators. One such function is the groupby function. This function groups data from the given iterable based on the key that is computed by a function.

Syntax

zip_groupby(iterable, key=None)

The function takes two arguments: iterables and fill value.

  • iterable is the data structure over which you want to iterate. It can be of any type such as a list, tuple, or dictionary.

  • key is a function that computes the key for each element in iterable.

Return value

The function returns an iterable object with consecutive keys and groups.

Example

  • In the following example, we first create an input_list that contains some universities and the programs offered.
  • We create the key_func function to compute the key based on which we will group the data.
  • grouped_data is returned by the groupby method which takes in the input_list and key_func to group data.
  • We iterate over the grouped_data and print the key and its corresponding value.
import itertools
input_list = [("University", "Harvard"),("University","MIT"),("University","UC Berkley"),("Program","Computer Science"),("Program", "Marine Biology")]
key_func = lambda x: x[0]
grouped_data = itertools.groupby(input_list,key_func)
for key,value in grouped_data:
print(key,":",list(value))

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources