Python : Remove all values from a list in between two values

Question:

I have a list of string values.

list1 = ["13:00","13:10","13:20","13:30","13:40"]
range_start = "13:10"
range_end = "13:30"

I want to remove all values (including range_start and range_end)that lie in between the ranges.

Asked By: Jagwant Sehgal

||

Answers:

EDIT:

Sorry, misread your question. I thought you only wanted to keep these values. Changed my code accordingly.

list1 = ["13:00","13:10","13:20","13:30","13:40"]
range_start = "13:10"
range_end = "13:30"

You can use list comprehension with the range condition:

list1 = [x for x in list1 if not(range_start<=x<=range_end)]
print(list1)

You could also use filter on your list:

list1=list(filter(lambda x:not(range_start<=x<=range_end), list1))
print(list1)
Answered By: Christian König
list1 = ["13:00","13:10","13:20","13:30","13:40"]
del list1[1:-1]
print(list1)
Answered By: İlaha Algayeva
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.