Sometimes, more than one type of data can come in Python list and sometimes it’s undesirably tokenized and hence we require to join the words that have been tokenized and leave the digits as they are. Let’s discuss certain ways in which this task can be achieved.
Method #1 : Using list comprehension +
This task can be performed using the list comprehension, first by joining the words and then joining the digits and then separating only the numbers, while joining to form the resultant string.
The original list : [‘Geeks’, ‘5’, ‘for’, ‘9’, ‘Geeks’, ‘2’, ‘5’]
The joined adjacent word list(ignoring digits) : [‘GeeksforGeeks’, ‘5’, ‘9’, ‘2’, ‘5’]
Method #2 : Using
itertools.chain.from_iterable() + groupby() + join()
This task can also be performed using the groupby function which groups the digits together and then
from_iterables function joins the list and characters together joined by the join function.
The original list : [‘Geeks’, ‘5’, ‘for’, ‘Geeks’, ‘2’, ‘3’]
The joined adjacent word list(ignoring digits) : [‘Geeks’, ‘5’, ‘forGeeks’, ‘2’, ‘3’]
- Python | Merge list of tuple into list by joining the strings
- Python | Joining unicode list elements
- Python | Find k longest words in given list
- Python | Spilt a sentence into list of words
- Python | Check if given words appear together in a list of sentence
- Python | Calculate difference between adjacent elements in given list
- Possible Words using given characters in Python
- Python | Extract words from given string
- Python | Number to Words using num2words
- Python | Stemming words with NLTK
- Reverse words in a given String in Python
- Python program to count words in a sentence
- Python | Scramble words from a text file
- Python | Toggle characters in words having same case
- Sort the words in lexicographical order in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.