"Python" thinking?

I’m learning python and probably wading in too quickly without really having the basics as second nature

lots of examples, but I think things like list comprehension and having intuitive grasp of how the data structures work with indexing and stuff, but just picking out one example

how do I train myself to not write functions like #1 below (my solution to an easy codewars problem) but right away think about solutions #2 or #3 which just seem better and more “pythony”.

def get_count(input_str):
    num_vowels = 0
    # your code here
    vowels = ["a","e","i","o","u"]
    for vowel in vowels:
        num_vowels += input_str.count(vowel)

    return num_vowels

def get_count2(inputStr):
    return sum(1 for let in inputStr if let in "aeiouAEIOU")

def get_count3(inputStr):
    return sum(c in 'aeiou' for c in inputStr)

It’ll just come through practice. Sites like Codewars and Exercism etc are really, really good for this. All the challenges are small and self contained and can be done multiple different ways. So you can go through them, then see how other people have approached them, modify your code, rinse and repeat. Keep reading up on Python best practices as well. And use forums like this one to ask questions. Joining a big Python community somewhere like Discord or Slack is really useful as well so you can chat when you need help or advice.

Edit: I’m not a Python programmer, so someone else might be able to answer this, but I assume there’s at least one good code quality tool available (of languages I use, JS has ESLint, Rust has Clippy, Elixir has Credo, Ruby has Rubocop). So you’d add the extension for it to your text editor, and it would advise you on whether your code conforms to best practices.

1 Like

thanks Dan, that’s good to know. It can be a bit dispiriting to see the better ways to do it thinking I must be missing something or am stuck thinking in an old fashioned brute force kind of way, but I will keep at it then and not be afraid to look at other peoples solutions and try to learn from them so that I can instinctively put together these kinds of expressions without thinking

Yeah, definitely don’t worry about writing imperative code – it works, and it’s really easy to understand! More “Pythonic” code that makes heavy use of itertools is really elegant but grokking it when you’re not used to it is going to take a bit of time. I only use Python from time to time, so what you’ve written as the first solution is what I’d prefer to see, because I don’t have to do any mental gymnastics to figure out what’s happening (whereas if I used it all the time, I’d be able to scan read the second two, which I can’t currently do).

1 Like

thank you so much for the friendly (and rapid!) feedback. I’ve done a bit of coding before many years ago (but not competently) but have gotten the bug a bit now so it’s fantastic to have all these great online resources and people who have done this a million times before but are still willing to help and advise


I have 500 plus problems solved in leetcode, 30+ competitions and i would probably write it like you did in a competition.

normally after i solve a problem i go back look at the top solutions and see if they seem better: cleaner, faster, less memory, etc.

or make it a 1-liner for fun.

most people are just trying to do a one liner because its an added challenge to a problem they already understand.

# clean readable
def get_count(s):
    res = 0
    vowels = 'aeiou'
    for chr in s:
        if chr in vowels: res += 1    
    return res
#one line
return sum([1 for chr in s if chr in "aeiou"])

This isn’t really why, not for “pythonic” code. The reason they are considered more “pythonic” is that in many circumstances, comprehensions are clearer than imperative loops (they are better at signalling the intent of the code and describing how it works), not because they’re clever code. The caveat is that they are generally only clearer if the reader/writer is familiar with Python

Not quite a one to one comparison, but by analogy in JavaScript, full use of array methods vs. loops and liberal use of destructuring could both be considered things that produce much clearer code if the reader/writer is familiar with JS.

That’s not to say people don’t use them to try to write clever-clever code, but that’s not the point of them. And I guess with competition coding problems you’re almost always going to write imperatively as the point is to write optimised code for the toy problems as quickly as possible, so I’m not sure that’s the best marker here; it’s not really reflective of real life so much.