r/PythonLearning • • 14d ago

What do you guys think?

Post image

I made a program that finds the second largest digit in a list.

Using the list method "remove()" and "max" function. Python is COOL!

88 Upvotes

58 comments sorted by

View all comments

Show parent comments

11

u/NoMakeSenseOk 14d ago

Or if you want to write fewer lines:

numbers = [3, 10, 21, 54]

print(sorted(numbers)[-2])

6

u/Potential_Fix_5007 14d ago

But you have to keep in mind, that sorted() creates a new list so the original list is still chaotic. That can be intentional.

2

u/NoMakeSenseOk 14d ago

I should've mentioned this, since every new list or copy thereof increases memory usage. For less memory usage we use references rather than copies.

2

u/Potential_Fix_5007 14d ago

You are right but as far as i know "memory usage" is a topic you dont need to keep an eye on while learning. Early projects will nearly always count as "low memory" so dont panic over this directly