How to check for whitespaces in a Python Lists?


Python language comes with different varieties of data structures and in this article, it deals with the list data structure. In lists, the elements are arranged in some order and they can undergo various methods for checking the whitespace or spaces. The elements inside the list can be a string or an empty string, it can also be a tab inside the string or any one space. Among the various methods given below, isspace() function is considered an easier one.

Approach

Approach 1 − Using the “re” module

Approach 2 − Using the iteration method

Approach 3 − Using the isspace() method

Approach 1: Python Program to categorize input data using re.compiler() method

The re module is imported to use the re.compiler() function to match the whitespaces with the regular expression of the given list.

Algorithm

  • Step 1 − The “re” module is imported to use the regular expression.

  • Step 2 − The input list is declared with elements to hold strings along the space.

  • Step 3 − In the list data structure, the re.compile() method is used to match the whitespace with the regular expression.

  • Step 4 − Along with the iteration process, the pattern search function will look over the list of values.

  • Step 5 − It returns "It does not contain whitespace" when the spacing is incorrect.

Example

#”re” library is imported
import re

#defining the list with strings along with whitespace
my_list = ['hello', 'world ', ' ', '\t']
pattern = re.compile(r'\s+')
for val in my_list:
   if pattern.search(val):
      print(f"'{val}' contains whitespace")
   else:
      print(f"'{val}' does not contain whitespace")

Output

'hello' does not contain whitespace
'world ' contains whitespace
' ' contains whitespace
'	' contains whitespace

Approach 2: Python Program to categorize input data using iteration method

The for loop will iterate through the list along with the split() and join() functions.

Algorithm

  • Step 1 − Elements holding strings such as "Welcome," "to," and "Tutorialspoint" is initialized in the input list.

  • Step 2 − The split() method in the list data structure is used to separate the given strings into characters.

  • Step 3 − The strings without any spaces are then joined using the join() function.

  • Step 4 − Print the string statements without any whitespace.

Example

#initializing the list with strings along with whitespace
list_1 = ['Welcome','to','Tutorialspoint']
#defining the empty list
new_list = []
#for loop is used to iterate through the given list
for space in list_1:
   # split() function is used to split the string 
   words = space.split()
   # joining the strings without too many spaces and using only single space
   new_space = ' '.join(words)
   # add the cleaned string to the new list
   new_list.append(new_space)
if new_list != list_1:
#returning the statement in the list
	print("yes, it contains whitespace")
else:
	print("no, it does not whitespace")

Output

no, it does not whitespace

Approach 3: Python Program to categorize input data using isspace() method

The method used to check the whitespaces in the given list of strings is isspace() function.

Algorithm

  • Step 1 − Elements carrying strings are used to initialize the input list.

  • Step 2 − The isspace() method in the list data structure is used to determine whether any whitespaces are accessible, and the new list is produced once any extra whitespaces have been removed.

  • Step 3 − The if statement is used to find whitespaces whenever the string "Welcome to Tutorialspoint" is mentioned at any index value.

  • Step 4 − If the only spaces between the words are blank ones, this method returns true.

  • Step 5 − Print the string statements without any whitespace.

Example

# The List data structure is initialized with Whitespaces
sen =  "Welcome to Tutorialspoint"
# Using the isspace() method to check for whitespaces in the list
if sen[7].isspace():
#returning the statement in the list
	print("yes, it contains whitespace")
else:
	print("no, it does not whitespace")

Output

 yes, it contains whitespace

Conclusion

The Python program is used to check for the whitespaces in the given list data structure. The whitespace can be in different ways like tabs, space, or even the “\n”- newline characters. While running complex programs, when the whitespaces are found they have to be removed for efficient running of the code.

Updated on: 29-Aug-2023

317 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements