Change language

Python | Remove a tuple from a list of tuples if it contains no characters

|

Example :

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

Method # 1: Using List Comprehension

# Python code to remove all of these
# items from a list of tuples
# which does not contain an alphabet.

  
# Initialization list

List = [( ’, ’ , 12 ), ( ’Paras’ , 5 ),

  ( ’ jain.’ , 11 ), ( ’...’ , 55 ) ,

( ’-Geek’ , 115 ), ( ’ Geeksfor’ , 115 ),

( ’:’ , 63 ), ( ’Data’ , 3 ), ( ’ -’ , 15 ),

( ’Structure’ , 32 ), ( ’Algo’ , 80 ),]

 
# Using the comprehension list

out = [(a, b) for a , b in List  

if any (c.isalpha () for c in a)]

  
# Printout

print (out)

Exit:

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

Method # 2: Using Regex

# Python code to remove all these
# elements from the list of tuples
# which does not contain an alphabet.

 
# Initialization list

List = [( ’,’ , 12 ), ( ’Paras’ , 5 ),

  ( ’jain.’ , 11 ), ( ’...’ , 55 ),

( ’-Geek ’ , 115 ), ( ’Geeksfor’ , 115 ),

( ’:’ , 63 ), ( ’ Data’ , 3 ), ( ’-’ , 15 ),

( ’Structure’ , 32 ), ( ’ Algo’ , 80 ),]

 
# Import

import re

 
# Using regular expressions

out = [t for t in List if re.search (r ’ w’ , t [ 0 ])]

 
# Printout

print (out)

Exit :

[(’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 of these
#ele cops from a list of tuples
# which does not contain an alphabet.

 
# Initialization list

List = [( ’,’ , 12 ), ( ’ Paras’ , 5 ),

( ’jain.’ , 11 ), ( ’...’ , 55 ),

( ’ -Geek’ 115 ), ( ’Geeksfor’ , 115 ),

( ’: ’ , 63 ), ( ’Data’ , 3 ), ( ’-’ , 15 ),

( ’ Structure’ , 32 ), ( ’Algo’ , 80 ),]

  
# Using the filter

out = filter ( lambda x: any (c.isalpha ()

  for c in x [ 0 ]), List )

 
# Convert to list

out = list (out)

 
# Output print

print (out)

Exit:

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

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically