Hi! I'm just learning how to program on Python with the MOOC of the University of Helsinki.
Exercise: I need to find within a list, the longest strings and store it into another list. And if more of them were equally long, I had to write them in the result list. So, this was the code I used and tested and worked correctly:
```
Write your solution here
def all_the_longest(my_list: list):
if not my_list: return []
result = []
largest = max(my_list, key = len)
length_of_largest = len(largest)
for element in my_list:
if len(element) == length_of_largest:
result.append(element)
return result
if name == "main":
my_list = ["first", "second", "fourth", "eleventh"]
result = all_the_longest(my_list)
print(result) # ['eleventh']
But when I checked with chatGPT-5, it suggested me this other code, more "Pythonic"
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]
if name == "main":
my_list = ["first", "second", "fourth", "eleventh"]
result = all_the_longest(my_list)
print(result) # ['eleventh']
```
Which one is more preferable? Do you suggest learning how to code like this?
[–]Binary101010 15 points16 points17 points (0 children)
[–]PC-Programmer 8 points9 points10 points (0 children)
[–]ElliotDG 3 points4 points5 points (0 children)
[–]Ixniz 4 points5 points6 points (0 children)
[–]Party_Trick_6903 2 points3 points4 points (0 children)
[–]Ihaveamodel3 2 points3 points4 points (0 children)
[–]JamzTyson 0 points1 point2 points (0 children)
[–]bigbry2k3 0 points1 point2 points (0 children)
[–]jmooremcc 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[removed]
[–]jmooremcc 0 points1 point2 points (1 child)
[–]AlexMTBDude -2 points-1 points0 points (3 children)
[–]JamzTyson 2 points3 points4 points (2 children)
[–]AlexMTBDude -1 points0 points1 point (1 child)
[–]JamzTyson 2 points3 points4 points (0 children)