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

23

u/NoMakeSenseOk 13d ago

You can keep the list intact and print the second-to-last value of a sorted list.

numbers = [3, 10, 21, 54]

numbers.sort()
print(numbers[-2])

11

u/NoMakeSenseOk 13d ago

Or if you want to write fewer lines:

numbers = [3, 10, 21, 54]

print(sorted(numbers)[-2])

6

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

How your input should look like?

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

7

u/mossse 13d ago

That wouldn't keep it intact because sort modifies the list. You shared another solution using sorted, which doesn't have this side effect.

1

u/NoMakeSenseOk 13d ago

Oh, thanks for the correction! I still got lots to learn as an intermediate.

3

u/Vigintillionn 13d ago

well, to be honest, their solution is more efficient being O(3n) ~ O(n) while yours is O(nlogn). Not that it makes much of a difference, for small lists O(nlogn) might even beat theirs but asymptotically theirs is more efficient

1

u/max96t 12d ago

I agree. The best would probably be using the builtin heapq.nlargest function ( which is O(n))

1

u/PureWasian 12d ago

What if the array is [0, 1, 2, 2]?

What if the list has 1mil numbers? Why would you sort for O(NlogN) when it can be found in O(N)?

1

u/Strong_Essay1176 12d ago

I hope you are not dev.

1

u/NoMakeSenseOk 12d ago

I'm indeed not a dev, no. Still learning.

2

u/Strong_Essay1176 12d ago

Just do a,b=numbers[0],numbers]0] In loop set max to a and prev max to b.

Any kind sort - is like rebuilding whole building just to open 2 doors.

1

u/Ma4r 12d ago

Funnily enough this has a worse big O complexity than the OP

1

u/ArtisticFox8 7d ago

For a long list of numbers, sorting can take time FYI.

Second largest could be done with one for loop..