Is there a way to remove items in a range in list? For example: a = [1,2,3,4,5]
. How to remove items from 3 to 5?
Asked
Active
Viewed 203 times
0

Raymond Hettinger
- 216,523
- 63
- 388
- 485

omkar pathak
- 73
- 1
- 2
- 8
-
yes, but first consider posting a minimal, complete, verifiable example of your gui – Crispin Mar 29 '17 at 11:41
-
1I don't think this is a duplicate because it filters on a criterion rather than the last *n* elements. – Raymond Hettinger Mar 29 '17 at 20:56
-
Reopened; the dupe and target are sufficiently different. – cs95 Jan 15 '18 at 02:53
2 Answers
3
Something like this should do the trick
[z for z in [1,2,3,4,5,6,7] if not 3<=z<=5]
Out[2]:
[1, 2, 6, 7]
If you want to make it more flexible can replace with variables depending on your needs which is simply done:
alist=[1,2,3,4,5,6,7]
lowerbound=3
upperbound=5
resultlist=[z for z in alist if not lowerbound<=z<=upperbound]
#result you want stored as 'resultlist'

Vlox
- 717
- 8
- 18
0
Yes, you can use a list comprehension to filter the data.
a = [1,2,3,4,5]
print([x for x in a if 3 <= x <= 5])

MSeifert
- 145,886
- 38
- 333
- 352

Raymond Hettinger
- 216,523
- 63
- 388
- 485