Python | Remove additional spaces in list
Sometimes, we have a list that contains strings and spaces in between them. We desire to have uniformity, so that later if we decide them, we just have single spaces between the lists. Hence its sometimes necessary to remove the additional unnecessary spaces between the words in a list.
Let’s discuss certain ways in which this can be done.
Method #1 : Using list comprehension + enumerate()
In this method we create a whole new list rather than modifying the original one, and check for element and its preceding element for spaces and add only add one occurrence of space and leaving the rest.
# Python3 code to demonstrate # removing multiple spaces # using list comprehension + enumerate() # initializing list of lists test_list = ["GfG", "", "", "", "", "is", "", "", "best"] # printing original list print("The original list : " + str(test_list)) # using list comprehension + enumerate() # removing multiple spaces res = [val for idx, val in enumerate(test_list) if val or (not val and test_list[idx - 1])] # printing result print("The list after removing additional spaces : " + str(res)) |
The original list : ['GfG', '', '', '', '', 'is', '', '', 'best'] The list after removing additional spaces : ['GfG', '', 'is', '', 'best']
Method #2 : Using list comprehension + zip() + list slicing
In this method, we take a pair at a time and check if it’s both elements are empty, if so, we discard it. If anyone is empty or both are non-empty, we keep it in the list.
# Python3 code to demonstrate # removing multiple spaces # using list comprehension + zip() + list slicing # initializing list of lists test_list = ["GfG", "", "", "", "", "is", "", "", "best"] # printing original list print("The original list : " + str(test_list)) # using list comprehension + zip() + list slicing # removing multiple spaces res = test_list[ : 1] + [j for i, j in zip(test_list, test_list[1 : ]) if i or j] # printing result print("The list after removing additional spaces : " + str(res)) |
The original list : ['GfG', '', '', '', '', 'is', '', '', 'best'] The list after removing additional spaces : ['GfG', '', 'is', '', 'best']
Recommended Posts:
- Python | Remove spaces from a string
- Python | Remove unwanted spaces from string
- Python | Remove spaces from dictionary keys
- Python | Remove all values from a list present in other list
- Python list remove()
- Python | Remove given element from the list
- Python | Remove last K elements of list
- Python | Remove Duplicates from a List
- Python | Remove None values from list
- Python | Remove consecutive duplicates from list
- Python | Remove all digits from a list of strings
- Python | Remove random element from list
- Python | Remove repeated sublists from given list
- Python | Remove all strings from a list of tuples
- Python | Remove all occurrences in nested 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 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.



