Open In App

Python | Remove tuple from list of tuples if not containing any character

Given a list of tuples, the task is to remove all those tuples which do not contain any character value. 

Example: 



Input: [(', ', 12), ('...', 55),
        ('-Geek', 115), ('Geeksfor', 115),]

Output: [('-Geek', 115), ('Geeksfor', 115)]

Method #1 : Using list comprehension 




# Python code to remove all those
# elements from list of tuple
# which does not contains any alphabet.
 
# List initialization
List = [(', ', 12), ('Paras', 5),
        ('jain.', 11), ('...', 55),
        ('-Geek', 115), ('Geeksfor', 115),
        (':', 63), ('Data', 3), ('-', 15),
        ('Structure', 32), ('Algo', 80),]
 
# Using list comprehension
out = [(a, b) for a, b in List
       if any(c.isalpha() for c in a)]
 
# Printing output
print(out)

Output

[('Paras', 5), ('jain.', 11), ('-Geek', 115), ('Geeksfor', 115), ('Data', 3), ('Structure', 32), ('Algo', 80)]

Time complexity: O(n), where n is the length of the List.
Auxiliary space: O(m), where m is the number of tuples that have at least one alphabet.

Method #2: Using Regex 




# Python code to remove all those
# elements from list of tuple
# which does not contains any alphabet.
 
# List initialization
List = [(', ', 12), ('Paras', 5),
        ('jain.', 11), ('...', 55),
        ('-Geek', 115), ('Geeksfor', 115),
        (':', 63), ('Data', 3), ('-', 15),
        ('Structure', 32), ('Algo', 80),]
 
# Importing
import re
 
# Using regex
out = [t for t in List if re.search(r'\w', t[0])]
 
# Printing output
print(out)

Output
[('Paras', 5), ('jain.', 11), ('-Geek', 115), ('Geeksfor', 115), ('Data', 3), ('Structure', 32), ('Algo', 80)]

Method 3: Using Filter and lambda 




# Python code to remove all those
# elements from list of tuple
# which does not contains any alphabet.
 
# List initialization
List = [(', ', 12), ('Paras', 5),
        ('jain.', 11), ('...', 55),
        ('-Geek', 115), ('Geeksfor', 115),
        (':', 63), ('Data', 3), ('-', 15),
        ('Structure', 32), ('Algo', 80),]
 
# Using filter
out = filter(lambda x:any(c.isalpha()
                for c in x[0]), List)
 
# Converting in list
out = list(out)
 
# Printing output
print(out)

Output
[('Paras', 5), ('jain.', 11), ('-Geek', 115), ('Geeksfor', 115), ('Data', 3), ('Structure', 32), ('Algo', 80)]

Method 4: Using recursive function.




# Python code to remove all those
# elements from list of tuple
# which does not contains any alphabet.
 
#using recursive function
def filter_list(tuples, filtered):
    if not tuples:
        return filtered
    if any(c.isalpha() for c in tuples[0][0]):
        filtered.append(tuples[0])
    return filter_list(tuples[1:], filtered)
 
 
 
# List initialization
List = [(', ', 12), ('Paras', 5),
        ('jain.', 11), ('...', 55),
        ('-Geek', 115), ('Geeksfor', 115),
        (':', 63), ('Data', 3), ('-', 15),
        ('Structure', 32), ('Algo', 80),]
 
# Using list comprehension
out = filter_list(List, [])
 
# Printing output
print(out)
#this code contributed by tvsk

Output
[('Paras', 5), ('jain.', 11), ('-Geek', 115), ('Geeksfor', 115), ('Data', 3), ('Structure', 32), ('Algo', 80)]

Time Complexity: O(n)
Auxiliary Space:O(n)

METHOD 5: Using for loop

Approach/Intuition:

The Approach uses a for loop to iterate over each tuple in the given list of tuples. For each tuple, it checks the length of the first element (i.e., the string), and if it is greater than zero, the tuple is appended to a new list. Finally, the new list is printed as the output.

Steps that were to follow the above approach:

Below is the code to implement the above steps:




input_list = [('-Geek', 115), ('Geeksfor', 115)]
 
output_list = []
 
for tup in input_list:
    if len(tup[0]) > 0:
        output_list.append(tup)
 
print(output_list)

Output
[('-Geek', 115), ('Geeksfor', 115)]

The time complexity of the program is O(n), where n is the length of the input list.

The space complexity of the program is O(m), where m is the length of the output list. This is because the program creates a new list to store the tuples that contain characters, and the size of this list depends on the number of tuples that pass the condition.


Article Tags :