Given a Tuple list, remove all tuples with all None values.
Input : test_list = [(None, 2), (None, None), (3, 4), (12, 3), (None, )]
Output : [(None, 2), (3, 4), (12, 3)]
Explanation : All None tuples are removed.
Input : test_list = [(None, None), (None, None), (3, 4), (12, 3), (None, )]
Output : [(3, 4), (12, 3)]
Explanation : All None tuples are removed.
Method #1 : Using all() + list comprehension
In this, we use all() to check for all None values for discarding and list comprehension does task of iteration.
Python3
test_list = [( None , 2 ), ( None , None ), ( 3 , 4 ), ( 12 , 3 ), ( None , )]
print ( "The original list is : " + str (test_list))
res = [sub for sub in test_list if not all (ele = = None for ele in sub)]
print ( "Removed None Tuples : " + str (res))
|
Output
The original list is : [(None, 2), (None, None), (3, 4), (12, 3), (None,)]
Removed None Tuples : [(None, 2), (3, 4), (12, 3)]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(m), where m is the number of non-None tuples in the input list.
Method #2 : Using filter() + lambda + all()
In this method, task of filtering None tuples is done using filter() and lambda function to provide None checking functionality using all().
Python3
test_list = [( None , 2 ), ( None , None ), ( 3 , 4 ), ( 12 , 3 ), ( None , )]
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda sub : not all (ele = = None for ele in sub), test_list))
print ( "Removed None Tuples : " + str (res))
|
Output
The original list is : [(None, 2), (None, None), (3, 4), (12, 3), (None,)]
Removed None Tuples : [(None, 2), (3, 4), (12, 3)]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n)
Method #3: Using count() method
If count of None in each tuple equals to length of tuple, then the entire tuple elements are None.Used this in if condition and appended other tuples to output list
Python3
test_list = [( None , None ), ( None , None ), ( 3 , 4 ), ( 12 , 3 ), ( None , )]
print ( "The original list is : " + str (test_list))
res = []
for i in test_list:
if not (i.count( None ) = = len (i)):
res.append(i)
print ( "Removed None Tuples : " + str (res))
|
Output
The original list is : [(None, None), (None, None), (3, 4), (12, 3), (None,)]
Removed None Tuples : [(3, 4), (12, 3)]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using enumerate function
Python3
test_list = [( None , None ), ( None , None ), ( 3 , 4 ), ( 12 , 3 ), ( None , )]
res = [sub for i,sub in enumerate (test_list) if not all (ele = = None for ele in sub)]
print (res)
|
Time Complexity: O(n) where n is the number of elements in the list “test_list”. enumerate function performs n number of operations.
Auxiliary Space: O(n), extra space is required
Method #5 : Using operator.countOf()
Python3
test_list = [( None , None ), ( None , None ), ( 3 , 4 ), ( 12 , 3 ), ( None , )]
print ( "The original list is : " + str (test_list))
res = []
import operator
for i in test_list:
if not (operator.countOf(i, None ) = = len (i)):
res.append(i)
print ( "Removed None Tuples : " + str (res))
|
Output
The original list is : [(None, None), (None, None), (3, 4), (12, 3), (None,)]
Removed None Tuples : [(3, 4), (12, 3)]
Time Complexity : O(N)
Auxiliary Space : O(N)
Method 6: Using a for loop and slicing
In this method, we use a for loop to iterate through the list and remove the None tuples by slicing the list.
Python3
test_list = [( None , None ), ( None , None ), ( 3 , 4 ), ( 12 , 3 ), ( None , )]
print ( "The original list is : " + str (test_list))
res = []
for i in range ( len (test_list)):
if None not in test_list[i]:
res.append(test_list[i])
print ( "Removed None Tuples : " + str (res))
|
Output
The original list is : [(None, None), (None, None), (3, 4), (12, 3), (None,)]
Removed None Tuples : [(3, 4), (12, 3)]
Time Complexity: O(n), where n is the length of the list
Auxiliary Space: O(n), where n is the length of the list (to store the result list)
Method 7: Using recursion
Step-by-step approach:
- Define the original list
- Define a recursive function that takes the original list and an empty list as arguments
- The function should check if the original list is empty
- If it is, return the empty list
- Otherwise, check if the first element of the list is a tuple containing None values
- If it is, call the function recursively with the remaining elements of the list and the empty list
- Otherwise, append the first element of the list to the empty list and call the function recursively with the remaining elements of the list and the updated list
- Call the recursive function with the original list and an empty list as arguments and assign the result to a new variabl
Python3
test_list = [( None , None ), ( None , None ), ( 3 , 4 ), ( 12 , 3 ), ( None , )]
def remove_none_tuples(lst, res):
if not lst:
return res
elif None in lst[ 0 ]:
return remove_none_tuples(lst[ 1 :], res)
else :
res.append(lst[ 0 ])
return remove_none_tuples(lst[ 1 :], res)
new_list = remove_none_tuples(test_list, [])
print ( "Removed None Tuples : " + str (new_list))
|
Output
Removed None Tuples : [(3, 4), (12, 3)]
Time complexity: O(n), where n is the length of the original list
Auxiliary space: O(n), where n is the length of the original list
Please Login to comment...