Remove elements within specific range from a list | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Remove elements within specific range from a list

I am new here. I'm trying to do below but can not make it to work in case: 1. a = b 2. a not followed by b 3. maybe few more scenarios Any help is highly appreciated. Below is my code: # Given a list of integers of any length, return a new list that does not contain any elements between the range a and b (inclusive) # a may equal to b # every a element may not be followed by b def popa2b(nums,a,b): list = [] ignore = False for i in range(len(nums)): if nums[i] == a: ignore = True if ignore: if nums[i] == b: ignore = False continue list.append(nums[i]) return list a = int(input("Enter a: ")) b = int(input("Enter b: ")) print(popa2b([1, 6, 2, 6, 2, 7, 1, 6, 99, 99, 7],a,b))

14th Jan 2020, 9:25 AM
Uncle 64
3 Answers
0
Are you talking about range as in the numbers between two numbers? Or are you talking about range as in between two indexes? By the way, can you place the code into a program in the Code Playground? It makes it easier to use, modify, and debug.
14th Jan 2020, 11:10 AM
Jianmin Chen
Jianmin Chen - avatar
0
Thanks for your assistance. I just put the code in the Code Playground: uncle64_popa2b.py The range shall be between the two numbers (a,b). popa2b([1, 6, 2, 6, 2, 7, 1, 6, 99, 99, 7],6,7) shall return: [1, 1]
15th Jan 2020, 7:01 AM
Uncle 64
0
Well, you got it!☺
15th Jan 2020, 11:13 AM
Jianmin Chen
Jianmin Chen - avatar