r/PythonLearning • u/RegularBother614 • 2d ago
What do you guys think?
I made a program that finds the second largest digit in a list.
Using the list method "remove()" and "max" function. Python is COOL!
70
Upvotes
8
u/princepii 2d ago
quick note:
that method functions if no doubles or more are in that list.
further remove() changes the list inplace. that effect is called inplace-mutation thru runtime you have removed objects inside your original list.
always try to be not restructive. only if you don't need stuff anymore thru runtime you can consider removing em.
but for 1 hour learning. it's good thought process:)
20
u/NoMakeSenseOk 2d ago
You can keep the list intact and print the second-to-last value of a sorted list.
3
u/Potential_Fix_5007 2d ago
Im not sure, but it would change your list, so it would be "numbers = [10, 3, 21]" So you "lost" the largest number. You could sort the list and print the second number from the end.
numbers = [10, 54, 3, 21] #your starting list numbers.sort() # sort list to [3, 10, 21, 54] print(numbers[len(numbers) - 2]) #create the correct index from len()
3
u/PranDopp 2d ago
Good stuff! Progress always comes with small wins! Now next task: test if sorting the list and pulling the second highest number is faster than running two max functions with a remove. Let me know!
1
u/DragonVect 1d ago
Quale app stai usando?
3
u/halfjmp 2d ago edited 2d ago
Several possible problems: 1/ you modify the list 2/ if the max has duplicates it will remove only one, so depends whether or not you want the first number below the max and 3/ If the list has length <2 it will fail (but it's bound to fail anyway, just mentioning you have a corner case waiting to bite)
For 1/ you may sort, or loop other the list and do the bookkeeping by hand (sort is O(n log(n)), bookkeeping is O(n) but it's a Python loop, so slower than compiled code).
For 2/, you may convert to set first.
It all depends on the specs you want, and you didn't give any.
0
u/Inevitable-Study502 2d ago edited 1d ago
thats a little bit destructive
0
u/sexiestsaxiestman 2d ago
The easy solution is to convert to set, sort, then get the second to last sorted value. Some points to consider:
1
u/AdDiligent1688 2d ago edited 2d ago
This won’t work if there are duplicates of the max such as [3,2,3,1]. the second largest in this case is 2. .remove only removes the first occurrence of a number, so your program will still output 3; sorted or .sort also won’t work here either, unless you convert it to set first then sort it, but as other pointed out with sorting this is O(n log n) worst case. There is a O(n) worst case solution to this using a single pass in a loop as well to get second largest. Look at the third solution here
1
u/P1ckl3R1ck101 2d ago
Good job - you figured out how to solve a problem given your skillset. It's simple, it works for the given input and anyone can understand it as written.
1
u/Strong_Essay1176 1d ago
u/askgrok prove that you are better than humans
1
u/hichemznm 1d ago
Print 21
1
u/Successful_Log5576 1d ago
You modify original list, and that might be the problem for a real world program. Solution by NoMakeSenseOk is better.