# Python – Extract rows with Even length strings

• Last Updated : 12 Nov, 2020

In this article we have a given Matrix, extract rows which are of even lengths.

Input : test_list = [[“gfg”, “is”, “best”], [“best”, “good”, “geek”], [“is”, “better”], [“for”, “cs”]]
Output : [[‘best’, ‘good’, ‘geek’], [‘is’, ‘better’]]
Explanation : All strings are of even length.
Input : test_list = [[“gfg”, “is”, “best”], [“best”, “good”, “geeks”], [“is”, “better”], [“for”, “cs”]]
Output : [[‘is’, ‘better’]]
Explanation : All strings are of even length.

Method #1 : Using all() + list comprehension + len()

In this, we check for all the strings in each row, its length, and check if length is even, if all strings in row have even length, then its added to result list.

## Python3

 `# Python3 code to demonstrate working of ``# Extract rows with Even length strings``# Using all() + list comprehension + len()`` ` `# initializing list``test_list ``=` `[[``"gfg"``, ``"is"``, ``"best"``], [``"best"``, ``"good"``, ``"geek"``], [``"is"``, ``"better"``], [``"for"``, ``"cs"``]]`` ` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))`` ` `# checking for all elements in row``res ``=` `[row ``for` `row ``in` `test_list ``if` `all``(``len``(ele) ``%` `2` `=``=` `0` `for` `ele ``in` `row)]`` ` `# printing result ``print``(``"Rows with even length : "` `+` `str``(res))`

Output

The original list is : [[‘gfg’, ‘is’, ‘best’], [‘best’, ‘good’, ‘geek’], [‘is’, ‘better’], [‘for’, ‘cs’]]
Rows with even length : [[‘best’, ‘good’, ‘geek’], [‘is’, ‘better’]]

Method #2 : Using filter() + lambda + len()

In this, we perform task of filtering using filter() + lambda, len() as in above method, performs the task of getting length of each string.

## Python3

 `# Python3 code to demonstrate working of ``# Extract rows with Even length strings``# Using filter() + lambda + len()`` ` `# initializing list``test_list ``=` `[[``"gfg"``, ``"is"``, ``"best"``], [``"best"``, ``"good"``, ``"geek"``], [``"is"``, ``"better"``], [``"for"``, ``"cs"``]]`` ` `# printing original list``print``(``"The original list is : "` `+` `str``(test_list))`` ` `# checking for all elements in row``# filtering done using filter() and lambda ``res ``=` `list``(``filter``(``lambda` `row : ``all``(``len``(ele) ``%` `2` `=``=` `0` `for` `ele ``in` `row), test_list))`` ` `# printing result ``print``(``"Rows with even length : "` `+` `str``(res))`

Output

The original list is : [[‘gfg’, ‘is’, ‘best’], [‘best’, ‘good’, ‘geek’], [‘is’, ‘better’], [‘for’, ‘cs’]]
Rows with even length : [[‘best’, ‘good’, ‘geek’], [‘is’, ‘better’]]

My Personal Notes arrow_drop_up