user3655039
user3655039

Reputation: 19

Successive minimum values in a list

I'm working on a project and I need to find the minimum value in a list successively. For example:

sample = [0, 4, 68, 6344, 342, 3, 32, 21, 215, 589]

Can you please show me how will it return the minimum value, excluding zero, which is 3, then afterwards, 4 until it becomes 6344? I can't use sort and then print it from index 0 to len(sample) because after each loop (in my project), the list will be appended and the order may be distorted.

Upvotes: 0

Views: 65

Answers (1)

sshashank124
sshashank124

Reputation: 32197

Why not just sort the list:

sample = [0, 4, 68, 6344, 342, 3, 32, 21, 215, 589]

mins = sorted(i for i in sample if i!=0)

>>> print mins
[3, 4, 21, 32, 68, 215, 342, 589, 6344]

OR

sample = [0, 4, 68, 6344, 342, 3, 32, 21, 215, 589]

mins = sorted(filter(lambda x: x!=0, sample))

>>> print mins
[3, 4, 21, 32, 68, 215, 342, 589, 6344]

Upvotes: 4

Related Questions