10 python idioms to help you improve your code

If you have ever tried to learn a new language (not a programming language), you know that we always think in our native language before we translate it to the new language. This can lead to you forming some sentences that don’t make sense in the new language but are perfectly normal in your native language. For example, in a lot of languages, you ‘open’ an electronic gadget such as fan, AC or cell phone. When you say that in English, it means to literally open the gadget instead of turning it on.

The same is true for programming languages. As we pick up new languages, such as python, we are using our prior knowledge of programming in another language (q, java, c++ etc) and translating that to python. Many times, your code will work but it won’t be ‘pretty’ or fast. In python terms, your code won’t be ‘pythonic’.

In this post, I would like to cover some python idioms that can be very helpful. These idioms will:

  1. Help your code look better,
  2. Speed up your code, and
  3. Set you apart from beginners

Let’s begin!

Note: All examples are written in python 2.

Update: Thanks to Diane and my other readers for pointing out some errors in my examples!

 

Idiom #1

Use tuples to swap values! In python, you do not need to create temp value when swapping variable values. They can be done in one line by using tuples.

In [ ]:
# bad
temp = old
old = new
new = temp

# good
old, new = new, old

Idiom #2

Don't use indices when looping. In python, you do not need to keep track of indices when looping.

In [2]:
states = ["texas", "california", "ohio", "florida"]

"""
# bad
i = 0
while i < len(states):
    print(states[i])
    i += 1
"""

# good
for state in states:
    print(state)
texas
california
ohio
florida

Idiom #3

Use 'enumerate' when you need to use the index.

In [3]:
# For the same example as above, what if you REALLY need to use the index? Here is how to do that:

states = ["texas", "california", "ohio", "florida"]
for index, state in enumerate(states):
    print(index)
    print(state)
0
texas
1
california
2
ohio
3
florida

Idiom #4

Use 'zip' to loop over two lists.

In [4]:
# What if you wanted to loop over two lists at once? Zip is the function to do just that.
states = ["texas", "california", "ohio", "florida"]
weather = ["hot", "medium", "no_idea", "hot"]
for weather, state in zip(weather, states):
    print(weather + ' in ' + state)
hot in texas
medium in california
no_idea in ohio
hot in florida

Idiom #5

Use 'izip' for long lists. 'zip' constructs the new combined list before entering the loop. For long lists, this new list can consume a lot of memory. Instead, use 'izip' which constructs the new list on-the-go.

Note: In python 3, zip returns an iterator. iZip does not exist in python 3.

In [ ]:
states = ["texas", "california", "ohio", "florida"]
weather = ["hot", "medium", "no_idea", "hot"]
for weather, state in izip(weather, states):
    print(weather + ' in ' + state)

Idiom #6

Use 'join' to join strings together. Strings have a built-in function called 'join' that can be used to concatenate them.

In [15]:
sample = ['my', 'name', 'is']

"""
#bad
sample[0] + ' ' + sample[1] + ' ' + sample[2]
"""

# good
' '.join(sample)
Out[15]:
'my name is'

Idiom #7

0 is False and everything else is True. Any string, list, array or dict with length > 0 is True. This applies to many other languages as well.

In [16]:
sample = ['mom', 'dad']
stri = "hi"
count = 0

"""
# Bad

if len(sample)>0:
    print('hi')

if len(stri)>0:
    print('hello')
    
if count == 0:
    print('nothing')

"""

# good

if sample:
    print('hi')

if stri:
    print('hi')

if not count:
    print('nothing')
hi
hi
nothing

Idiom #8

Use 'format' for string formatting. It's easier to read and less prone to error.

In [18]:
character = 'Neo'
movie = 'Matrix'
actor = 'Keanu'

"""
#bad

print('%s played %s in %s.' % (actor, character, movie))

"""

# good

print('{actor} played {character} in {movie}'.format(actor=actor,
                                                    character=character,
                                                    movie=movie))
Keanu played Neo in Matrix

Idiom #9

Use list comprehensions to generate a list. Most of the times, they are easier to read and require fewer words.

In [6]:
states = ["texas", "california", "ohio", "florida"]

"""
# bad

new = []
for state in states:
    if 'a' in state:
        new.append(state)

"""

# good

new = [state for state in states if 'a' in state]

print new
['texas', 'california', 'florida']

Idiom #10

Generators are a powerful concept in python. Instead of having a function which returns a huge list at once, use generators to return one value at a time as it's called. Creating a generator is easy, just use 'yield' instead of 'return'. A key thing to remember is that a generator returns a generator object which is an iterrator.

Note: In python 3, range returns an iterator.

In [34]:
# Function for getting a list of numbers
# This function will return a generator
import datetime as dt
def get_range(num):
    for n in range(0,num):
        yield n

print get_range(5)
<generator object get_range at 0x10410df50>
In [37]:
# call the generator object so we can iterate over it
for num in get_range(5):
    print(num)
0
1
2
3
4

 

I would highly recommend looking at your existing code and figuring out where you can use these idioms to make your code cleaner and faster.

This is definitely not an exhaustive list of idioms. There are many more that I didn’t cover or am not aware of. If you know any I missed, feel free to leave a comment.

 

14 responses to “10 python idioms to help you improve your code”

  1. Robert Gathergood says:

    Thank you Himanshu
    Speaking as someone who is returning to coding after an absence of more than 20 years I found your article very interesting and useful as I am currently learning python, and my previous experience in the 80s and early 90s was limited to BASIC, PASCAL, COBOL and a little 6502 and Z80 assembler.

    • Himanshu says:

      You’re welcome! I am glad you liked it. I am also relatively new to python and like to post about what I have learned to help other learners.

      • Mohammad Ghoreishi says:

        Thanks for your post. I am new in python. what do you suggest for me to learn more?

        • Himanshu says:

          Mohammad – Thanks! You can look at the ‘Resources’ page where I mention some good resources for learning python/pandas.

  2. Kavin says:

    Really much needed information man.. Thank you…!

  3. Untitled says:

    Keep on writing, great job!

  4. Diane Chen says:

    Great choice of Python idioms. I would suggest that you differentiate between Python 2 & Python 3. Your use of print() makes it look like it might be Python 3, though most apply to both.

    In Python 3, zip() is a generator, so you can combine #4 & #5 into one, or perhaps call out that #5 is only for Python 2. A nice Python 3 zip() example is making a dictionary using a comprehension:
    {state : temp for state, temp in zip(states, weather)}

    In #10, range() in Python 3 is already a generator. Please pick a better example, since generators are totally awesome!

    Please fix the “bad” examples in #7 to correctly reflect the use of truthiness in the “good” examples.

    “””
    # Bad

    if len(sample) > 0:
    print(‘hi’)

    if len(stri) > 0:
    print(‘hello’)

    if count == 0:
    print(‘nothing’)

    “””

    • Himanshu says:

      Thanks Diane. These examples were written using python 2. You’re right, I should state that clearly to avoid any confusion. I will make the change soon.

  5. Luigi Bidoia says:

    Thank you Himanshu.
    your tips is very useful. I have written a python course using jupyter notebook for italian people. I would like to write a notebook about python idioms. May i use your tips?

  6. kabangu says:

    I have teaching myself python for a year now. this article is so nicely and clear explained.Well done. thank you

  7. Mori says:

    Useful for me. Thanks

  8. John E Dizaro says:

    Great tips! Its usefull for me.

Leave a Reply

Your email address will not be published. Required fields are marked *