Monday 3 October 2022

itertools.groupby in Python

 Lets share our insight, Today i will share for you about itertools.groupby() function in Python Language. Oke let learn.

Itertools.groupby() This method calculates the keys for each element present in iterable. It returns key and iterable of grouped items.


For example :

import itertools
a = "teestiiiingproogramsss"
x = itertools.groupby(a)
for k,g in x:
    print(k, list(g))


Output :

t ['t']
e ['e', 'e']
s ['s']
t ['t']
i ['i', 'i', 'i', 'i']
n ['n']
g ['g']
p ['p']
r ['r']
o ['o', 'o']
g ['g']
r ['r']
a ['a']
m ['m']
s ['s', 's', 's']


Thanks for visiting my sites, hope it useful

Source : https://www.codespeedy.com/itertools-groupby-in-python/
              https://www.geeksforgeeks.org/itertools-groupby-in-python/


No comments:

Post a Comment