Change language

Python | Check if the suffix matches any string in the given list

|

Examples :

  Input:  lst = ["Paras", "Geeksforgeeks", "Game"], str = ’Geeks’  Output:  True  Input:  lst = ["Geeks", "for", "forgeeks"], str = ’John’  Output:  False 

Let’s discuss a few methods to do the task.

Method # 1: Using any()
The shortest and a readable way to check if a suffix exists in a list of strings, — use any ().

# Python code to check
# suffix exists in list of strings.

 
# Initialize the input list

lst = [ "Paras" , "Geeksforgeeks" , "Game" ]

 
# using any to find the suffix

Output = any ( ’Geek’ in x for x in lst)

 
# Print

print ( "Initial List is: " , lst)

print (Output )

Exit:

 Initial List is: [’Paras’,’ Geeksforgeeks’, ’Game’] True 

Method # 2: Using filter () and lambda
This is another way to accomplish this particular task using lambda ().

# Python code to check
# suffix exists in the list lines.

 
# Initialize the input list

lst = [ "Paras" , "Geeksforgeeks" , "Game" ]

 
# Using filter and lambda

Output = len ( list ( filter ( lambda x : "Jai" in x, lst)))! =   0

 
# Printout

print ( "Initial List is:" , lst)

print (Output)

Exit:

 Initial List is : [’Paras’,’ Geeksforgeeks’, ’Game’] False 

Python | Check if the suffix matches any string in the given list Python functions: Questions

Python | Check if the suffix matches any string in the given list String Variables: Questions

Shop

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

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method