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.
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 +
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.
The original list is : ['g', '1', '2', 'i', '5', 'b', '6', '7'] List after digit merge : ['g', '12', 'i', '5', 'b', '67']
- Python - Merge consecutive empty Strings
- Python | Ways to merge strings into list
- Python - Consecutive element deletion strings
- Python | Pair the consecutive character strings in a list
- Python Program to Count number of binary strings without consecutive 1's
- Python | Remove all digits from a list of strings
- Python | Split strings and digits from string list
- Python | Merge list of tuple into list by joining the strings
- Python | Remove empty strings from list of strings
- Python | Merge Python key values to list
- Python | Tokenizing strings in list of strings
- Python | Merge key value lists
- Python | Merge list elements
- Python PIL | Image.merge() method
- Python | Merge elements of sublists
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.