Change language

Python | Remove the first K elements that meet some condition

|

Method # 1: The Naive Method
We can add elements that match the condition after K element occurrences have been performed, and therefore perform a task similar to deletion.

# Python3 demo code
# remove the first match condition K elements
# using the naive method

 
# initializing list

test_list = [ 3 , 5 , 1 , 6 , 7 , 9 , 8 , 5 ]

 
# print original list

print ( "The original list is:" + str (test_list))

  
# using the naive method
# remove the first K elements match condition
# removes the first 4 odd occurrences

counter = 1

res = []

for i in test_list:

if counter"  4 or not (i % 2 ! = 0 ):

res.append (i)

  else :

  counter + = 1

 
# print result

print ( "The filtered list is:" + str (res))  

Exit:

 The original list is: [3 , 5, 1, 6, 7, 9, 8, 5] The filtered list is: [6, 9, 8, 5] 

Method # 2: Using itertools.filterfalse () + itertools.count ()
This is a different and elegant way to accomplish this particular task. It filters out all numbers that get larger than K when the counter reaches K and meets the condition. This is a one-liner and the preferred method for this task.

# Python3 demo code
# remove the first K elements match condition
# using itertools.filterfalse () + itertools.count ()

from itertools import filterfalse, count

 
# initializing list

test_list = [ 3 , 5 , 1 , 6 , 7 , 9 , 8 , 5 ]

 
# prints the original list

print ( "The original list is: " + str (test_list))

 
# using itertools.filterfalse () + itertools.count ()
# remove the first K elements match condition
# remove the first 4 odd occurrences

res = filterfalse ( lambda i, counter = count (): i % 2 ! = 0 and

  next (counter) & lt;  4 , test_list)

 
# print result

print ( "The filtered list is:" + str ( list (res)))

Exit:

 The original list is: [3, 5, 1, 6, 7 , 9, 8, 5] The filtered list is: [6, 9, 8, 5] 

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