Ocean community
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
There is no need to loop, I assume you get these numbers from an array? Then you can sort them and get the second element. Something like this:
list_numbers = [24, 10, 60, 25, 9]
# sorting the list
list_numbers.sort()
print("The second largest element of the list is:", list_numbers[-2])
Hi there,
Here is another option to what has already been mentioned:
list = [9, 6, 4, 10, 13, 2, 3, 5]
max = float('-inf')
second_last = float('-inf')
for x in list:
if x > max:
second_last = max
max = x
elif x > second_last and x != max:
second_last = x
print(second_last)
Hope that this helps!
Best,
Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.