Ad
  • Default User Avatar

    I agree with you, it was just a trick to lose some code. A lot of the solutions here are not production standard, often more pared down or employing tricks.

  • Custom User Avatar

    Note that "code that is clear to beginner" does not mean "code that looks pretty much like imperative, C code".

    This is a very common beginner mistake by itself, so saying that sentence would be quite hypocritical ;-)

  • Default User Avatar

    I would never write this code in a project with other members

    When you'll be used to python, you'll change your mind about that... ;) List or generator comprehensions are, when not too much convoluted and with meaningful variable names, easier to read because you read those like you would say your algo out loud:

    "get the name for all the names in the list of personns if the name contains 5 letters at least"
    
    => filtered = [name for name in lstName if len(name) >= 5]
    

    And this is SO MUCH simpler/shorter than:

    filtered = []
    for name in lstName:
      if len(name) >= 5:
        filtered.append(name)
    
  • Custom User Avatar

    The reason it is best practice, is because it is idiomatic Python code. It's neat, self contained, and self explanitory. And most importantly comes naturally with familiarity of the language.

    As a beginner, you may be unfamilar with comprehensions, in which case the solution may initially look a little alien, I had that difficulty when I first took up the Python, especially with the "if" coming after the "for loop".

    However what you are sugesting - that all code be written so beginners are able to read it, would mean that new constructs could never become common usage, and the language syntax would be complete upon first release.

    I'd argue that there is some middle ground, beginners attempt to learn what they don't understand, and the more experienced don't write incomprehensibly complex code - just because they can.

    In this case however, I'd argue that 1000+ identical correct submissions can't all be wrong. :)