Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to check for whitespaces in a Python Lists?
Python lists can contain strings with various types of whitespace characters like spaces, tabs, or newlines. This article explores three different approaches to detect whitespace in list elements: using regular expressions, string manipulation, and the isspace() method.
Using Regular Expressions
The re module provides pattern matching to detect any whitespace character using \s+ regex pattern ?
import re
# List with various whitespace types
my_list = ['hello', 'world ', ' ', '\t', 'python\n']
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")
'hello' does not contain whitespace 'world ' contains whitespace ' ' contains whitespace ' ' contains whitespace 'python ' contains whitespace
Using String Methods
Check if strings contain extra whitespace by comparing original with cleaned versions ?
# List with strings containing whitespace
original_list = ['Welcome ', ' to', ' Tutorialspoint ', 'Python']
for item in original_list:
# Remove extra whitespace
cleaned = ' '.join(item.split())
if item != cleaned:
print(f"'{item}' contains extra whitespace")
else:
print(f"'{item}' has no extra whitespace")
'Welcome ' contains extra whitespace ' to' contains extra whitespace ' Tutorialspoint ' contains extra whitespace 'Python' has no extra whitespace
Using isspace() Method
The isspace() method returns True if a string contains only whitespace characters ?
# List with different types of elements
test_list = ['hello', ' ', '\t', '\n', ' ', 'world', '']
for item in test_list:
if item.isspace():
print(f"'{repr(item)}' is only whitespace")
elif any(char.isspace() for char in item):
print(f"'{item}' contains some whitespace")
else:
print(f"'{item}' has no whitespace")
'hello' has no whitespace ' ' is only whitespace '\t' is only whitespace '\n' is only whitespace ' ' is only whitespace 'world' has no whitespace '' has no whitespace
Checking Entire List for Whitespace
Function to check if any element in a list contains whitespace ?
def has_whitespace_elements(lst):
whitespace_items = []
clean_items = []
for item in lst:
if any(char.isspace() for char in item):
whitespace_items.append(item)
else:
clean_items.append(item)
return whitespace_items, clean_items
# Test the function
sample_list = ['hello', 'world ', ' test', '\tpython', 'code']
with_space, without_space = has_whitespace_elements(sample_list)
print("Items with whitespace:", with_space)
print("Items without whitespace:", without_space)
Items with whitespace: ['world ', ' test', '\tpython'] Items without whitespace: ['hello', 'code']
Comparison
| Method | Best For | Detects |
|---|---|---|
| Regular Expression | Complex patterns | Any whitespace anywhere |
| String Methods | Extra/multiple spaces | Unnecessary whitespace |
| isspace() | Whitespace-only strings | Pure whitespace elements |
Conclusion
Use regular expressions for comprehensive whitespace detection, string methods for cleaning extra spaces, and isspace() for identifying pure whitespace elements. Choose the method that best fits your specific whitespace checking requirements.
