Hello! I am new to all of this; sometimes I seem to come up with a different way to code an exercise that works - but later I find out it’s undesirable due to some hidden rule that hasn’t been described. (Previously I had a list removing items from itself within a for loop while it’s iterating and was told that’s a bad idea)
Anyway. For this exercise, what Mosh did in 6 lines I did in just one… and it seems to work. Can anyone tell me if there’s a reason why this wouldn’t be a good idea?
My version: (line 3)
His version: (lines 3 through 8)
Reading other people’s code will always be a good idea. So, I learned something from your code today. Tks for that
That being said, and answering your question, NO. If it works, and the result is correct, it doesn’t matter how you code it. 2 brains have different perspectives, and therefore, different solutions, so as long as the result is correct, do as you fit better!
Something worth metioning here is that maybe Mosh is trying to teach how to iterate, and what you are doing is using a method… which are 2 different things, and when you are learning to code, learning the very basics is a must! The hardest part of coding is THINKING… think of a way to solve the problem you are facing… and for that you have to understand the very basics. SO, perhaps Mosh is trying to do that in the course. Let’s say (and consider it a coding exercise) that Mosh wants to count the characters in that sentence WITHOUT the blank spaces , In this case, perhaps yo have to use a different approach to the one you used. Use anything, but provide the correct result (it would be nice to see your code on this, so post it here ).
So, keep coding. Follow Mosh code and examples and then implement your own way. The most important thing here is to UNDERSTAND what you are doing. The goal is to get a correct answer; if you can do it writing less code, that’s great!, but always keep an eye on the Zen of Python: Simple is better than complex.
Hope to see your code.
Well… that depends on what you want to do later with the list. So, it’s a bad idea if you need the original list later. But if you need to delete some records (because they are useless), go ahead.
I once wrote a program that went thru an Excel file extracting all the info and putting it into a list. From the list I had to use only certain items if a certain condition was met… So, why keeping info that I was not using???
That depends on the problem you are facing.
Thanks for the replies!
This exercise came along right after Mosh taught us about dictionary comprehensions, so that’s what I used and then was suprised they weren’t in his solution! I guess I just found a different way. Good to know! Cheers!