Solution
What I want is to save the values 0, 76, 176, 262, and 349.
So it sounds like you want to iterate over the list, and if the current element is greater than its preceding element by 50 then save it to the list and repeat. This solution assumes the original list is sorted. Let's turn this into some code.
lst = [0, 76, 91, 99, 176, 192, 262, 290, 349]
new_lst = []
for i, num in enumerate(lst):
if i == 0 or num - lst[i-1] > 50:
new_lst.append(num)
print(new_lst)
# [0, 76, 176, 262, 349]
The interesting part here is the conditional within the loop:
if i == 0 or num - lst[i-1] > 50:
The first part considers if we're at the first element, in which case I assume you want to add the element no matter what. Otherwise, get the difference between our current element and the previous element in the original list, and check if the difference is greater than 50. In both cases, we want to append the element to the list (hence the or).
Notes
- You should avoid using
list
as a variable so you don't shadow the built-in Python type.
lst.extend([num])
is better written as lst.append(num)
.
enumerate(lst)
allows you to easily get both the index and value of each element in a list (or any iterable).