Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Solution

Hassan K
Hassan K
1,556 Points

My code was significantly longer than the code in the video, would it be considered "wrong" or not pythonic?

if __name__ != "__main__":
    def min_finder(k):
        i = 0
        for k[i] in k:
            if k[i] == min(k):
                print (f"The lexicographically first word is {k[i]}")
            else:
                i += 1

2 Answers

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,074 Points

Not exactly, whether code is "long" or not doesn't mean the solution is "wrong". Typically the most important thing when determining if a solution is the wrong one or not is whether or not it accomplishes the task it set out to do. After that it's performance.

If the code you wrote accomplishes the task and performs well (and not worst than another solution), then it comes down to a compromise between efficiency (line numbers) and maintainability (readability/extendability). There is a fad in the python community of writing these "elegant" one-liners that can do a lot of things in one line of code. While this may be neat to flex your python skills they typically make for bad maintainable code, so I'd refrain from that.

But little rant out of the way, nothing about your code would be wrong or non-pythonic just because it has a few more lines than someone else's solution. It's just another solution among a multitude each with their own benefits and trade-offs.

arthur alonso
seal-mask
.a{fill-rule:evenodd;}techdegree
arthur alonso
Python Development Techdegree Student 1,810 Points

Here is my answer based on a function just introduced in the last course right before this workshop on 'beginning python' track, "Introducing Dictionaries" by my favorite teacher, Ashley Boucher!

def first_finder(list):
    return sorted(list)[0]

Just wanted to thank her!

Marc Roths
Marc Roths
1,571 Points

Hi Arthur! This was my solution as well. I am however, not willing to acknowledge a favorite teacher just yet lol.

Ali Colak
Ali Colak
2,192 Points

Hey Arthur, what does the list[0] do in this case? , wouln't it keep it at the first item in the list?

Rachel Johnson
Rachel Johnson
Treehouse Teacher

Ali Colak that's correct! This function aims to find the first in an alphabetical list.

sorted(list) makes sure it's sorted alphabetically first, and [0] grabs the first element. This then gets returned, fulfilling the function's goal.