r/PythonLearning • • 11d 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!

89 Upvotes

58 comments sorted by

View all comments

Show parent comment

11

u/NoMakeSenseOk 11d ago

Or if you want to write fewer lines:

numbers = [3, 10, 21, 54]

print(sorted(numbers)[-2])

6

u/Potential_Fix_5007 11d 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 11d 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 11d 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

3

u/RegularBother614 11d ago

Thanks! Is there any way I can ask for user input and store it in a variable list?

variable_name = input() variable_name = [ ]

2

u/Potential_Fix_5007 11d ago

my_list = []

my_list.append[input("Enter a number:")]

Like this the Program will wait untill the User enters something, but keep in mind that input() will always return a string so if you need a number you need tho convert it.

1

u/Money_Ad_5277 11d ago

How your input should look like?

If you just want a list with single user input, you could do variable_name = [input()]