programming-examples/python/List/Python program to find the second largest number in a list.py

13 lines
355 B
Python
Raw Normal View History

2019-11-15 12:59:38 +01:00
def second_largest(numbers):
count = 0
n1 = n2 = float('-inf')
for x in numbers:
count += 1
if x > n2:
if x >= n1:
n1, n2 = x, n1
else:
n2 = x
return n2 if count >= 2 else None
print(second_largest([1, 2, -8, -2, 0]))