Python – Filter rows with only Alphabets from List of Lists

PythonServer Side ProgrammingProgramming

When it is required to filter the rows that contains only alphabets in a list of lists, the list is iterated over and the ‘isalpha’ method is used to check if an alphabet is present or not.


Below is a demonstration of the same

my_list = [["python", "is", "best"], ["abc123", "good"],["abc def ghij"], ["abc2", "gpqr"]]

print("The list is :")
my_result = [sub for sub in my_list if all(element.isalpha() for element in sub)]

print("The result is :")


The list is :
[['python', 'is', 'best'], ['abc123', 'good'], ['abc def ghij'], ['abc2', 'gpqr']]
The result is :
[['python', 'is', 'best']]


  • A list of list elements is defined that contains string values.

  • This is displayed on the console.

  • The elements are iterated over and checked to see if they are alphabets.

  • This is done using the ‘isalpha’ method.

  • The results are assigned to a variable.

  • This variable is displayed as output on the console.

Published on 20-Sep-2021 06:45:59