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’]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Merge list of tuple into list by joining the strings
- Python | Joining unicode list elements
- Joining Threads in Python
- Joining Excel Data from Multiple files using Python Pandas
- Tableau - Joining data files with inconsistent labels
- Joining two Pandas DataFrames using merge()
- Joining NumPy Array
- Python | Spilt a sentence into list of words
- Python | Find k longest words in given list
- Python | Check if given words appear together in a list of sentence
- Python - Remove words containing list characters
- Python - Remove duplicate words from Strings in List
- Python | Extract Nth words in Strings List
- Python - Extract words starting with K in String List
- Python - List Words Frequency in String
- Python Program that Extract words starting with Vowel From A list
- Python | Initialize a dictionary with only keys from a list
- Python | Add only numeric values present in a list
- Python program to extract only the numbers from a list which have some specific digits
- Python | Calculate difference between adjacent elements in given list
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.