Python - Remove rows with Numbers
Last Updated :
22 Apr, 2023
Given a Matrix, remove rows with integer instances.
Input : test_list = [[4, 'Gfg', 'best'], ['gfg', 5, 'is', 'best'], [3, 5], ['GFG', 'Best']]
Output : [['GFG', 'Best']]
Explanation : All rows with numbers are removed.
Input : test_list = [[4, 'Gfg', 'best'], ['gfg', 5, 'is', 'best'], [3, 5], ['GFG', 'Best', 1]]
Output : []
Explanation : All rows with numbers are removed. No list left.
Method #1 : Using any() + list comprehension
In this, we check for any integer element using any() in any row, and use list comprehension to perform task of iteration.
Python3
# Python3 code to demonstrate working of
# Remove rows with Numbers
# Using list comprehension + any
# initializing lists
test_list = [[4, 'Gfg', 'best'], [
'gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
# printing original lists
print("The original list is : " + str(test_list))
# using isinstance to check for integer and not include them
res = [sub for sub in test_list if not any(
isinstance(ele, int) for ele in sub)]
# printing result
print("The filtered rows : " + str(res))
Output:
The original list is : [[4, 'Gfg', 'best'], ['gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']] The filtered rows : [['gfg', 'is', 'best'], ['GFG', 'Best']]
Method #2 : Using filter() + lambda + any()
In this, we perform task of filtering using lambda and filter(), any() is used in similar way as in above method.
Python3
# Python3 code to demonstrate working of
# Remove rows with Numbers
# Using filter() + lambda + any()
# initializing lists
test_list = [[4, 'Gfg', 'best'], [
'gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
# printing original lists
print("The original list is : " + str(test_list))
# using isinstance to check for integer and not include them
# filter() used to filter with lambda fnc.
res = list(filter(lambda sub: not any(isinstance(ele, int)
for ele in sub), test_list))
# printing result
print("The filtered rows : " + str(res))
Output:
The original list is : [[4, 'Gfg', 'best'], ['gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']] The filtered rows : [['gfg', 'is', 'best'], ['GFG', 'Best']]
Method #3 : Using list(),map(),join() and isalpha() methods
Python3
# Python3 code to demonstrate working of
# Remove rows with Numbers
# initializing lists
test_list = [[4, 'Gfg', 'best'], [
'gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
# printing original lists
print("The original list is : " + str(test_list))
res=[]
for i in test_list:
a=list(map(str,i))
a="".join(a)
if(a.isalpha()):
res.append(i)
# printing result
print("The filtered rows : " + str(res))
OutputThe original list is : [[4, 'Gfg', 'best'], ['gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
The filtered rows : [['gfg', 'is', 'best'], ['GFG', 'Best']]
Method #4 : Using list(),map(),join(),replace() and len() methods
Python3
# Python3 code to demonstrate working of
# Remove rows with Numbers
# initializing lists
test_list = [[4, 'Gfg', 'best'], [
'gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
# printing original lists
print("The original list is : " + str(test_list))
res=[]
numbers="0123456789"
for i in test_list:
a=list(map(str,i))
a="".join(a)
x=a
for j in numbers:
a=a.replace(j,"")
if(len(x)==len(a)):
res.append(i)
# printing result
print("The filtered rows : " + str(res))
OutputThe original list is : [[4, 'Gfg', 'best'], ['gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
The filtered rows : [['gfg', 'is', 'best'], ['GFG', 'Best']]
The time complexity of this code is O(n*m), where n is the length of the input list, and m is the length of the longest row in the list.
The auxiliary space complexity of this code is O(n), where n is the length of the input list.
Method#5:Using a loop and isnumeric()
Algorithm:
- Initialize an empty list "res"
- Loop through each row of the input list "test_list"
- Convert each element of the row to a string using the "map" function and the "str" method
- Concatenate the string elements of the row using the "join" method to create a single string "a"
- Check if the string "a" contains only alphabetic characters using the "isalpha" method
- If the string contains only alphabetic characters, append the row to the "res" list
- Return the filtered rows list "res"
- Print the filtered rows list
Python3
test_list = [[4, 'Gfg', 'best'], ['gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
# printing original lists
print("The original list is : " + str(test_list))
res = []
for sublist in test_list:
flag = True
for ele in sublist:
if isinstance(ele, int) or ele.isnumeric():
flag = False
break
if flag:
res.append(sublist)
print("The filtered rows : " + str(res))
#This code is contributed by Vinay Pinjala.
OutputThe original list is : [[4, 'Gfg', 'best'], ['gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
The filtered rows : [['gfg', 'is', 'best'], ['GFG', 'Best']]
Time complexity: O(n*m) where n is the number of rows and m is the number of columns in the input list "test_list"
Auxiliary Space: O(1) (ignoring the space required to store the filtered rows list)
Method 6: using the set() function and set operations.
Here are the steps:
- Define the input list test_list.
- Convert each sublist in test_list into a set using the set() function. This will remove duplicates and convert all elements into a set of unique values.
- Check if the set contains any integer or numeric values using the any() function and the isdigit() string method. The any() function returns True if at least one element in an iterable is True, and False otherwise.
- If the set does not contain any integers or numeric values, append the original sublist to a new list res.
- Print the filtered sublists.
Python3
# Define input list
test_list = [[4, 'Gfg', 'best'], ['gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
# Initialize empty list for filtered sublists
res = []
# Iterate over each sublist in test_list
for sublist in test_list:
# Convert sublist to set
set_sublist = set(sublist)
# Check if set contains any integers or numeric values
if not any(str(element).isdigit() or isinstance(element, int) for element in set_sublist):
# Append original sublist to res if set does not contain integers or numeric values
res.append(sublist)
# Print filtered sublists
print("The filtered rows: ", res)
OutputThe filtered rows: [['gfg', 'is', 'best'], ['GFG', 'Best']]
Time complexity: O(n*m), where n is the length of test_list and m is the length of the longest sublist in test_list.
Auxiliary space: O(m), because it uses a set to store each sublist and the size of each set is at most the length of the longest sublist in test_list.
Method 7: using reduce():
- Import the reduce() function from the functools module.
- Define the list of lists test_list.
- Define a lambda function that takes an accumulator acc and a current list x as input. If all elements in x are non-numeric strings, the lambda function appends x to acc. Otherwise, it returns acc as is.
- Use reduce() to apply the lambda function to each sublist in test_list. The initial value of the accumulator is an empty list.
- Store the resulting filtered list in the variable res.
- Print the original list and the filtered list.
Python3
from functools import reduce
# Driver Code
test_list = [[4, 'Gfg', 'best'], [
'gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
# User reduce() to filter the list of lists
res = reduce(lambda acc, x: acc + [x] if all(isinstance(ele, str)
and not ele.isnumeric() for ele in x) else acc, test_list, [])
# print original list and filtered rows
print("The original list is : " + str(test_list))
print("The filtered rows : " + str(res))
OutputThe original list is : [[4, 'Gfg', 'best'], ['gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
The filtered rows : [['gfg', 'is', 'best'], ['GFG', 'Best']]
Time complexity:
The lambda function is applied to each sublist in test_list exactly once. The time complexity of the lambda function is O(m), where m is the length of the sublist. Therefore, the time complexity of the reduce() function is O(nm), where n is the length of test_list.
Space complexity:
The space complexity of this code is O(n), where n is the length of test_list. This is because we create a new list for each sublist that satisfies the condition in the lambda function, and the maximum number of sublists that can satisfy this condition is n
Similar Reads
Python - Remove numbers with repeating digits
Given a list of numbers, the task is to write a Python program to remove all numbers with repetitive digits. Examples: Input : test_list = [4252, 6578, 3421, 6545, 6676]Output : test_list = [6578, 3421]Explanation : 4252 has 2 occurrences of 2 hence removed. Similar case for all other removed. Input
5 min read
Python - Remove Keys with K value
We are given a dictionary we need to remove the keys with K value. For example, we are having a dictionary d = {'a': 1, 'b': 2, 'c': 1, 'd': 3} we need to remove the keys which is having K value so that the output should be {'b': 2, 'd': 3} . We can use dictionary comprehension and many other method
3 min read
Python Remove Set Items
Python sets are an efficient way to store unique, unordered items. While adding items to a set is straightforward, removing items also offers a variety of methods. This article will guide you through the different techniques available to remove items from a set in Python.Remove single set item using
3 min read
Python - Remove positional rows
Given a Matrix, the task is to write a Python program to remove rows that have certain positions. Example: Input: test_list = [[3, 5, 2], [1, 8, 9], [5, 3, 1], [0, 1, 6], [9, 4, 1], [1, 2, 10], [0, 1, 2]]; idx_lis = [1, 2, 5] Output: [[3, 5, 2], [0, 1, 6], [9, 4, 1], [0, 1, 2]] Explanation: 1st, 2nd
7 min read
Python - Filter Rows with Range Elements
Given a Matrix, filter all the rows which contain all elements in the given number range. Input : test_list = [[3, 2, 4, 5, 10], [3, 2, 5, 19], [2, 5, 10], [2, 3, 4, 5, 6, 7]], i, j = 2, 5 Output : [[3, 2, 4, 5, 10], [2, 3, 4, 5, 6, 7]] Explanation : 2, 3, 4, 5 all are present in above rows. Input :
5 min read
Python - Retain Numbers in String
Retaining numbers in a string involves extracting only the numeric characters while ignoring non-numeric ones.Using List Comprehensionlist comprehension can efficiently iterate through each character in the string, check if it is a digit using the isdigit() method and join the digits together to for
2 min read
Python program to remove row with custom list element
Given a matrix, the task here is to write a Python program to remove rows that have any element from the custom list and then display the result. Examples: Input : test_list = [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]], check_list = [3, 10, 19, 29, 20, 15] Output : [[7, 8, 9], [12, 18, 21]] E
6 min read
Python - Remove Rows for similar Kth column element
Given a Matrix, remove row if similar element has occurred in row above in Kth column. Input : test_list = [[3, 4, 5], [2, 3, 5], [10, 4, 3], [7, 8, 9], [9, 3, 6]], K = 2 Output : [[3, 4, 5], [10, 4, 3], [7, 8, 9], [9, 3, 6]] Explanation : In [2, 3, 5], we already has list [3, 4, 5] having 5 at K, i
7 min read
Python | Remove similar element rows in tuple Matrix
Sometimes, while working with data, we can have a problem in which we need to remove elements from the tuple matrix on a condition that if all elements in row of tuple matrix is same. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + all() This ta
6 min read
Python - Remove the row if all elements equal to N
Sometimes, while handling data, especially in the Machine Learning domain, we need to go through a lot of similar N-equal data. We sometimes need to eliminate the rows which are all equal to N. Letâs discuss certain ways to remove the rows that have all N values as list columns. Method #1: Using lis
5 min read