Python | Remove all lines from a list of tuples

Python Methods and Functions

Examples :

  Input:  [(1, 'Paras'), (2,' Jain'), (3, 'GFG '), (4,' Cyware')]  Output:  [(1), (2), (3), (4)]  Input:  [('string ',' Geeks'), (2, 225), (3, '111')]  Output:  [(), (2, 225), (3,)] 

Method # 1: Using the filter () method

# Python code to remove all lines from the list of tuples

 
# Check function to check instance

def check (x):

return not isinstance (x, str )

 
# create a list of tuples

listOfTuples = [( ' string' , 'Paras' ), ( 2 , 'Jain' ), ( 3 , 'GFG' ),

( 4 , 'Cyware' ), ( 5 , 'Noida' )] 

 
# using phil ьтр

output = ([ tuple ( filter ( check, x)) for x in listOfTuples])

 
# print output

print (output)

Exit:

 [(), (2,), (3,), (4,), (5,)] 

Method # 2:

# Python code to remove all lines from list of tuples

 
# Create a list of tuples

listOfTuples = [( 'string' , ' Geeks' ), ( 2 , 225 ), ( 3 , '111' ),

( 4 , 'Cyware' ), ( 5 , 'Noida' )] 

 

 

output = [ tuple (j for j in i if not isinstance (j, str ))

  for i in listOfTuples]

  
# printout

print ( output)

Exit:

 [ (), (2, 225), (3,), (4,), (5,)] 




Get Solution for free from DataCamp guru