I've been working through the problems on LeetCode in Python and occasionally I'll see comments saying that someone's solution is "Pythonic". I guess it means that your code embodies the spirit of the language of Python, but what specific aspects and conventions does that refer to?
Asked
Active
Viewed 438 times
0
-
for example, `for i in range(len(lst))` is c style, `for item in lst` is Pythonic. and list comprehension is also Pythonic. – Lei Yang Mar 21 '22 at 02:22
-
using the for clause is probably the most pythonic thing. Ima dditionseapping values as such a,b = b,a is considered pythonic – Fishball Nooodles Mar 21 '22 at 02:27
-
@FishballNooodles yes, that is a good example, using the old temporary variable construct *does work*, e.g. `temp = a; a = b; b = a` *works*, but it is more idiomatic to simply use `a, b = b, a` – juanpa.arrivillaga Mar 21 '22 at 02:36
1 Answers
0
Well answered here
But I would add that being pythonic is more than just to "Look like" python. Languages are designed in ways so that being some-language-ic makes the compiler/interpreter more efficient and less error-prone.

Gawain
- 71
- 6
-
2You should vote to close, not post a link to another question/answer as an answer – juanpa.arrivillaga Mar 21 '22 at 02:33
-
he doesn't have the previledge to vote to close, and nees some reputation to have that so answered. – Lei Yang Mar 21 '22 at 02:57
-
@LeiYang Flagging as duplicate is possible with 15 reputation, I think. – Kelly Bundy Mar 21 '22 at 03:46