# Python | Consecutive characters frequency

Sometimes, while working with Python, we can have a problem in which we need to compute the frequency of consecutive characters till the character changes. This can have applications in many domains. Let us discuss certain ways in which this task can be performed in Python.

### Count Consecutive Characters using list comprehension + groupby()

This is one of the shorthand with the help of which this task can be performed. In this, we employ groupby() to group consecutive together to perform frequency calculations.

## Python3

 `from` `itertools ``import` `groupby`   `# initializing string` `test_str ``=` `"geekksforgggeeks"`   `# printing original string` `print``(``"The original string is : "` `+` `test_str)`   `# Consecutive characters frequency` `res ``=` `[``len``(``list``(j)) ``for` `_, j ``in` `groupby(test_str)]`   `# printing result` `print``(``"The Consecutive characters frequency : "` `+` `str``(res))`

Output

```The original string is : geekksforgggeeks
The Consecutive characters frequency : [1, 2, 2, 1, 1, 1, 1, 3, 2, 1, 1]

```

### Count Consecutive Characters frequency using a loop

Here, we are using a for loop to iterate through the input string. For each character in the input string, we compare it with the next character in the string using an if statement. If the current character is the same as the next character, we increment the count variable by 1. If the current character is different from the next character, we append the current value of count to the res list and reset the value of count to 1. After iterating through the entire string, we append the final value of the count to the res list. Finally, we print the result using the print statement with a message.

## Python3

 `# initializing string` `test_str ``=` `"geekksforgggeeks"`   `# printing original string` `print``(``"The original string is : "` `+` `test_str)`   `# Consecutive characters frequency using loop` `res ``=` `[]` `count ``=` `1` `for` `i ``in` `range``(``len``(test_str)``-``1``):` `    ``if` `test_str[i] ``=``=` `test_str[i``+``1``]:` `        ``count ``+``=` `1` `    ``else``:` `        ``res.append(count)` `        ``count ``=` `1` `res.append(count)`   `# printing result` `print``(``"The Consecutive characters frequency : "` `+` `str``(res))`

Output

```The original string is : geekksforgggeeks
The Consecutive characters frequency : [1, 2, 2, 1, 1, 1, 1, 3, 2, 1, 1]

```

### Python Consecutive identical elements using regex

Another way to solve this problem is using regex. In this, we employ the regex character-finding technique and find the count using len()

## Python3

 `import` `re`   `# initializing string` `test_str ``=` `"geekksforgggeeks"`   `# printing original string` `print``(``"The original string is : "` `+` `test_str)`   `# Consecutive characters frequency Using regex` `res ``=` `[``len``(sub.group()) ``for` `sub ``in` `re.finditer(r``'(.)\1*'``, test_str)]`   `# printing result` `print``(``"The Consecutive characters frequency : "` `+` `str``(res))`

Output

```The original string is : geekksforgggeeks
The Consecutive characters frequency : [1, 2, 2, 1, 1, 1, 1, 3, 2, 1, 1]

```

### Using itertools.groupby() to Count Consecutive Characters frequency

Import the itertools module. Initialize the input string. Use the itertools.groupby() function to group the consecutive characters in the string. Convert the grouped characters into a list and get the length of the list for each group of consecutive characters. Store the length of each group in a list. Print the list of lengths as the result.

## Python3

 `import` `itertools`   `# initializing string` `test_str ``=` `"geekksforgggeeks"`   `# printing original string` `print``(``"The original string is : "` `+` `test_str)`   `# Consecutive characters frequency Using itertools.groupby()` `res ``=` `[``len``(``list``(group)) ``for` `key, group ``in` `itertools.groupby(test_str)]`   `# printing result` `print``(``"The Consecutive characters frequency : "` `+` `str``(res))`

Output

```The original string is : geekksforgggeeks
The Consecutive characters frequency : [1, 2, 2, 1, 1, 1, 1, 3, 2, 1, 1]

```

### Count Consecutive Characters using Counter() function

Import the collections module to use the Counter() function. Initialize an empty list ‘res’ to store the frequency of consecutive characters. Use the Counter() function to count the occurrence of each character in the string. Use a loop to iterate through the string, and check if the current character is equal to the next character. If it is, increment the count of consecutive characters, else append the count to the ‘res’ list, and reset the count to 1. Append the last count to the ‘res’ list. Print the ‘res’ list.

## Python3

 `from` `collections ``import` `Counter`   `test_str ``=` `"geekksforgggeeks"`   `# printing original string` `print``(``"The original string is : "` `+` `test_str)`   `# Consecutive characters frequency using Counter() and loop` `res ``=` `[]` `count ``=` `1`   `# using Counter() to count occurrence of each character` `c ``=` `Counter(test_str)`   `# iterating through the string` `for` `i ``in` `range``(``len``(test_str)``-``1``):` `    ``# checking if the current character is equal to the next character` `    ``if` `test_str[i] ``=``=` `test_str[i``+``1``]:` `        ``count ``+``=` `1` `    ``else``:` `        ``res.append(count)` `        ``count ``=` `1`   `res.append(count)`   `# printing result` `print``(``"The Consecutive characters frequency : "` `+` `str``(res))`

Output

```The original string is : geekksforgggeeks
The Consecutive characters frequency : [1, 2, 2, 1, 1, 1, 1, 3, 2, 1, 1]

```

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next