Change language

Python | Filter a list based on a given list of strings

| |

Examples :

  Input:  List_string1 = [’key’,’ keys’, ’keyword’,’ keychain’, ’keynote’ ] List_string2 = [’home / key / 1.pdf’,’ home / keys / 2.pdf’, ’home / keyword / 3.pdf’,’ home / keychain / 4.pdf’, ’home / Desktop / 5 .pdf’, ’home / keynote / 6.pdf’]  Output:  [’ home / Desktop / 5.pdf’]  Explanation:  We filter only those element from list_string2 that do not have string in list_string1 

Here are some ways to achieve the above task.

Method # 1: Using iteration

# Python code to filter an item from the list
# based on a different list of strings.

 
# Initialization list

Input = [ ’key’ , ’keys’ , ’ keyword’ , ’keychain’ , ’ keynote’ ]

Input_string = [ ’home / key / 1.pdf’ ,

  ’home / keys / 2.pdf’

’home / keyword / 3.pdf’

’home / keychain / 4.pdf’ ,

’home / Desktop / 5.pdf’

’home / keynote / 6.pdf’ ]

 

Output = Input_string.copy ()

temp = []

 
# Using iteration

for elem in Input_string:

for n in Input :

if n in elem:

temp. append (elem)

 

for elem in temp:

  if elem in Output:

Output.remove (elem)

 
# Print

print ( " List of keywords are: " , Input )

print ( "Given list:"  , Input_string)

print ( "filtered list is:" , Output)

Exit :

List of keywords are: [’key’, ’keys’,’ keyword ’,’ keychain ’,’ keynote ’]
Given list: [’ home / key / 1.pdf ’,’ home / keys / 2.pdf ’,’ home / keyword / 3.pdf ’,’ home / keychain / 4.pdf ’,’ home / Desktop / 5.pdf ’,’ home / keynote / 6.pdf ’]
filtered list is: [’ home / Desktop / 5.pdf ’]

Method # 2: Using List Comprehension

# Python code to filter an element from the list
# based on another list of strings.

 
# Initialization list

Input = [ ’key’ , ’keys’ , ’keyword’ , ’ keychain’ , ’keynote’ ]

Input_string = [ ’home / key / 1.pdf’ ,

’home / keys / 2.pdf’

’home / keyword / 3.pdf’

’home / keychain / 4.pdf ’ ,

  ’home / Desktop / 5.pdf’

  ’home / keynote / 6.pdf’ ]

 
# Using the comprehension list

Output = [b for b in Input_string if

all (a not in b for a in Input )]

 
# Print

print ( "List of keywords are:" , Input )

print ( "Given list : " , Input_string)

print ( "filtered list is:" , Output)

Exit :

List of keywords are: [’key’, ’keys’, ’keyword’ , ’keychain’, ’keynote’]
Given list: [’home / key / 1.pdf’, ’home / keys / 2.pdf’, ’home / keyword / 3.pdf’, ’home / keychain / 4.pdf ’,’ home / Desktop / 5.pdf ’,’ home / keynote / 6.pdf ’]
filtered list is: [’ home / Desktop / 5.pdf ’]

Python | Filter a list based on a given list of strings filter: Questions

Python | Filter a list based on a given list of strings Python functions: 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