# Python | Merge Consecutive digits Strings

Sometimes, while workings with data, we can have a problem in which we need to peform a merge of digits. This is a simpler problem. But it becomes complex once we need to perform merger of certain elements like numbers that too consecutive. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using list comprehension + `groupby() + isdigit()`
The combination of above functions can be used to perform this task. In this, we first group all the digits consecutive and then perform the concatenation.

 `# Python3 code to demonstrate  ` `# Merge Consecutive digits Strings ` `# using list comprehension + groupby() + isdigit() ` `from` `itertools ``import` `groupby ` ` `  `# Initializing list ` `test_list ``=` `[``'gfg'``, ``'1'``, ``'2'``, ``'is'``, ``'5'``, ``'best'``, ``'6'``, ``'7'``] ` ` `  `# printing original list ` `print``(``"The original list is : "` `+` `str``(test_list)) ` ` `  `# Merge Consecutive digits Strings ` `# using list comprehension + groupby() + isdigit() ` `res ``=` `[sub ``for` `i, j ``in` `groupby(test_list, ``str``.isdigit) ``for` `sub ``in` `([''.join(j)] ``if` `i ``else` `j)] ` ` `  `# printing result  ` `print` `(``"List after digit merge : "` `+` `str``(res)) `

Output :

```The original list is : ['gfg', '1', '2', 'is', '5', 'best', '6', '7']
List after digit merge : ['gfg', '12', 'is', '5', 'best', '67']
```

Method #2 : Using regex + `join()`
The combinations of above functions can also be used to deal with this. In this, we find the digits using regex function and perform merger using join(). Works only with single characters.

 `# Python3 code to demonstrate  ` `# Merge Consecutive digits Strings ` `# using regex() + join() ` `import` `re ` ` `  `# Initializing list ` `test_list ``=` `[``'g'``, ``'1'``, ``'2'``, ``'i'``, ``'5'``, ``'b'``, ``'6'``, ``'7'``] ` ` `  `# printing original list ` `print``(``"The original list is : "` `+` `str``(test_list)) ` ` `  `# Merge Consecutive digits Strings ` `# using regex() + join() ` `res ``=` `re.findall(``'\d+|.'``, ''.join(test_list)) ` ` `  `# printing result  ` `print` `(``"List after digit merge : "` `+` `str``(res)) `

Output :

```The original list is : ['g', '1', '2', 'i', '5', 'b', '6', '7']
List after digit merge : ['g', '12', 'i', '5', 'b', '67']
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.