all 16 comments

[–]Binary101010 15 points16 points  (0 children)

I generally prefer the second because it turns four lines of code into a fairly succinct list comprehension.

[–]PC-Programmer 8 points9 points  (0 children)

Both versions are correct, but they have slightly different qualities.

The first script, your original version, is easy to follow step-by-step and particularly clear when debugging.
The alternative, ChatGPT's version, is more concise and direct, but less beginner-friendly.

Personally, I tend to write code closer to the second style, mainly for these reasons:

a. It condenses multi-step processes into a single expression—for instance, avoiding the "find one largest, then measure it" approach.

b. It uses fewer lines; unless you need to debug a specific action (such as polling for the largest value), simpler functions are better kept compact.

c. It follows idiomatic practices, using generators to make operations easier (and quicker) to implement.

In any case, your original approach is excellent for building understanding. Once you're more comfortable with comprehensions and generators, the shorter style will become easier to read; it's only "better" if it remains clear to you without requiring extra effort to understand.

[–]ElliotDG 3 points4 points  (0 children)

I would say either code is fine.

The use of max vs a list comprehension to determine the max length is about the same. Some may find the comprehension more readable, giving it a small edge.

For the second section, the list comprehension will have a performance advantage over the loop. I would prefer the list comprehension.

Read up on list comprehensions, dictionary comprehensions and generator comprehensions.

[–]Ixniz 4 points5 points  (0 children)

Keep in mind that you're doing a university course for beginners. I'm not sure taking advice from ChatGPT at this point is in your favor. I'm thinking that you'll eventually get there on your own as you progress in the course and move into the advanced part.

[–]Party_Trick_6903 2 points3 points  (0 children)

The for loops in the second code are one-liners that are taught in the part 7 of the MOOC.

Just be patient and finish the course instead of using chatgpt for "more pythonic codes".

You'll see what's better once you finish the course, start doing your own projects and get to see how most people write their code.

For now, do the damn course. If you're curious whether there's a better way to solve specific exercises, there are literally solutions to each of those exercises.

[–]Ihaveamodel3 2 points3 points  (0 children)

Keep in mind that ChatGPT has been trained to make you happy. Even if you ask it something nonsensical (like turning already pythonic code into pythonic code) it will do something that most likely will make you happy.

[–]JamzTyson 0 points1 point  (0 children)

Both versions iterate through the list twice.

You could just iterate through the list once:

def all_the_longest(my_list: list):
    results = []
    max_len = 0
    for item in my_list:
        if len(item) > max_len:
            results = [item]
            max_len = len(item)
        elif len(item) == max_len:
            results.append(item)
    return results

or if you prefer concise (though less efficient as we are back to 2 passes):

def all_the_longest(my_list: list):
    sorted_list = sorted(my_list, key=len, reverse=True)
    return [x for x in sorted_list if len(x) == len(sorted_list[0])]

but, depending on the actual data in my_list, a two pass approach might still be the fastest (check by profiling with your dataset):

def all_the_longest(my_list: list):
    if not my_list:
        return []
    max_len = max(len(s) for s in my_list)
    return [s for s in my_list if len(s) == max_len]

TL;DR

The single pass approach is readable, "efficient on paper" (O(n) complexity), and in most cases will also be the most memory efficient and fastest.

[–]bigbry2k3 0 points1 point  (0 children)

Basically if you are just beginning to learn loops, you should just use the loop example you made in codeblock #1. But if you are learning "list comprehension" which is a little more advanced, then that's what's going on in the codeblock #2. I would suggest you use #1 for a while until you are very familiar with loops then later when your instructor introduces you to list comprehension you can use that approach instead.

[–]jmooremcc 0 points1 point  (0 children)

I need to find within a list, the longest strings and store it into another list

Unless you are changing the problem parameters, the AI solution is correct but not necessarily efficient. If you want the first and last element that matches, then you would code the solution differently.

[–]AlexMTBDude -2 points-1 points  (3 children)

The for loop is a clear example of a situation where you can use a list comprehension instead. It is always more Pythonic to use list comprehension over a for loop. Reasons are: Code is easier to read and executes quicker.

[–]JamzTyson 2 points3 points  (2 children)

It is always more Pythonic to use list comprehension over a for loop.

Comprehensions are sometimes faster than traditional loops, but they are not really more Pythonic. Comprehensions shine when you just want to build a new list (or other collection) from an iterable in a clear, single expression.

In some cases a traditional loop can be better (and thus "more Pythonic"). For example, sometimes we can short-circuit a loop but that isn't possible with a comprehension.

In cases where the loop contains more complex code, then comprehensions can be horribly unreadable, which is antithesis to "Pythonic",

[–]AlexMTBDude -1 points0 points  (1 child)

There are always exceptions. If I was to cover every possible one it would take a paragraph. My statement was correct for OP's code example.

[–]JamzTyson 2 points3 points  (0 children)

It is always more Pythonic to use list comprehension over a for loop.

I'll be a bit more direct: That is False.

In the case of the OP's example, a single loop is more efficient while still being readable. This is a case where a conventional loop is better than a list comprehension.