One of the biggest tenants of programming in Python is writing code that's as understandable, but as concise as possible. It's so important that we have the term Pythonic to describe code that's written this way. But we all sometimes fall into bad habits when we're in a rush to release new features and sometimes our code gets a bit... messy. Here are 30 ways you can think about refactoring your code to make it easier to understand, easier to work with, and just all around a bit more Pythonic. 1) Merge your nested if conditions Too much nesting in your code can make it tricky to understand. Especially in Python where we don't use brackets to help out with delineation of different levels of nesting. We can easily reduce the levels of nesting we have to deal with by combining two or more conditions with an . if and a: b: c if if return Can easily be condensed down to a and b: c if return 2) Don't repeat yourself! Hoist duplicate code out of conditionals We want to avoid duplicate code where possible and one of the most common places this crops up is within conditionals. If code is repeated in both/all branches of a conditional it's always going to execute, so we can hoist it out of the conditional and only used once. sold > DISCOUNT_AMOUNT: total = sold * DISCOUNT_PRICE label = f : total = sold * PRICE label = f if 'Total: {total}' else 'Total: {total}' The is getting used no matter what. So we can just pop it out of the conditional altogether. And we get the added bonus that it's now going to be clearer what the conditional actually controls for. label = f'Total: {total}' sold > DISCOUNT_AMOUNT: total = sold * DISCOUNT_PRICE : total = sold * PRICE label = f if else 'Total: {total}' 3) Fewer loops - part 1. Use any() instead of for loop We'll often want to check whether or not a condition holds for one or every item in a collection. Usually the way we see people go about this is with a for loop: found = False thing things: thing == other_thing: found = True for in if break This gets the job done, but there's a much much more concise way to do it! Python has built-in and functions that can easily check if a single element evaluates to or if every element evaluates to . So we can refactor this example to: any() all() True True found = any(thing == other_thing thing things) for in 4) Keep your loops clean. Hoist statements out of for/while loops Loops are almost always complex and tricky to follow. Adding in additional information to a loop that doesn't need to be there just makes the loop a bit more complex - so hoisting it outside the loop is a great opportunity to make things clearer! building buildings: city = addresses.append(building.street_address, city) for in 'London' Here the variable is assigned in the loop. But, it's only read, not altered at all - so we can pull it out of the loop altogether. city city = building buildings: addresses.append(building.street_address, city) 'London' for in 5) Fewer loops - part 2. Using list/set/dictionary comprehensions Another common task we find ourselves doing often when programming is creating a collection of values. In most languages we could do something like: cubes = [] i range( ): cubes.append(i ** ) for in 20 3 We define an empty list and then iteratively fill it with our values. This isn't in Python, but there's a much nicer way to do it using built-in comprehensions. They let us simplify the entire list creation into a single line and cut out the need to define the empty list. Our example would become: cubes wrong cubes = [i ** i range( )] 3 for in 20 Three lines down into one line is a nice win. Plus once you get used to the list comprehension syntax it actually is much more readable than the looped method. 6) Quick wins. Augmented expressions Whenever we're dealing with updating the value of a variable relative to itself we can get a nice little simplification by using Python's expressions. Instead of: count = count + other_value We can just write: count += other_value It's not a huge change, but it's a bit shorter and more concise. Plus we only have to think about the variable once. We can also use it with a bunch of other operators - , , , and . count -= *= /= **= Another common scenario we'll all deal with is looking to set a variable to one of two different values depending on some state. For example: 7) If expressions instead of if statements condition: x = : x = if 1 else 2 With an if expression we can cut that down to a single line: x = condition 1 if else 2 This refactoring is a bit conditional because some developers don't find if expressions clearer than if statements. But in general I think that if the if expression is short and fits on one line then it's a definite improvement. But feel free to debate me on this one :). 8) A better loop counter - enumerate When we're iterating over a list we often want a counter to let us know the index of the current item. Usually we'll see them written manually like: i = currency currencies: print(i, currency) i += 0 for in 1 But, Python has a built in function, , that lets us generate the index automatically & lets us save two lines of code: enumerate i, currency enumerate(currencies): print(i, currency) for in 9) Fewer loops - part 3. Using sums instead of loops So often we'll have to add up a list of things. Thankfully Python has a built-in function specifically for this. So instead of: sum() total = hat hats: total += hat.price 0 for in We can just write: total = sum(hat.price hat hats) for in Not only is it shorter (which we love) but it's also more descriptive about what the line of code is actually doing - finding the sum of the price of hats. 10) Another quick win. Simplifying sequence comparisons Before we do something to a list or sequence we often want to check if there are any elements there. One way it's often done is by checking if the length of the list is greater than 0: len(list_of_hats) > : hat_to_wear = choose_hat(list_of_hats) if 0 But Python automatically evaluates lists and sequences to if they have any elements and if they don't. This means the more Pythonic way of doing things is: True False len(list_of_hats): hat_to_wear = choose_hat(list_of_hats) if It's a bit less cluttered to do things this way, plus it's following the conventions set out in Python's guidelines. PEP8 There are tons of different ways to make code more Pythonic and easier to work with, these are just some of the most common examples we come across at Sourcery. This is a consolidated list of our we first published on our blog. Let me know if you disagree with any of these - or if there are any other common refactorings you think are really important! broader refactorings series