Python one line function guide

There are two ways you can create a one line function in Python:

  1. Get the function body in the same line as the header
  2. Use a lambda function

This tutorial shows you examples of how to create a one line function in practice.

1. Writing function header and body in one line

Although indentations matter in Python, you can compress a function into a single line by writing the function body next to the colon (:) symbol:

def function_name(): body

Let’s see an example. Suppose you have a function as follows:

def greet(name):
    print(f"Hello, {name}!")

You can turn it into a one line function as follows:

def greet(name): print(f"Hello, {name}!")

But please note that you can’t use this one liner if you have a control flow in your function, such as for and if-else statements.

Suppose you have a function that prints out a list as follows:

def greet(friends): 
    for friend in friends:
        print(f"Hello, {friend}!")

greet(["John", "Jane", "Chris"])

# Hello, John!
# Hello, Jane!
# Hello, Chris!

Then using this syntax causes an error:

def greet(friends): for friend in friends:  print(f"Hello, {friend}!")
File "main.py", line 1
   def greet(friends): for friend in friends:  print(f"Hello, {friend}!")
                       ^^^
SyntaxError: invalid syntax

You can’t put a for or if statement next to the colon (:) symbol. You need to use a list comprehension to get a for loop in one line.

Here’s an example:

def greet(friends): [print(f"Hello, {friend}!") for friend in friends]

The above function prints the items in the list and returns a list filled with None. The returned list is simply ignored in this case.

Still, this syntax is limited and unpythonic. Python conventions generally discourage multiple statements per line and the maximum line length is 79 characters.

Another approach to create a one line Python function is to use a lambda function. Let’s learn how to do it next.

2. Using a lambda function

A lambda function is an anonymous function intended to be an inline parameter of another function.

But this function can also be used to create a function and assign it to a variable. Here’s a simple example:

greet = lambda name: print(f"Hello, {name}!")

greet("John")  # Hello, John!

As you can see, the greet variable effectively becomes a function, and you actually create a one line function that doesn’t violate Python’s PEP 8 style guide.

But keep in mind that a lambda function can’t have a for loop in its body. Suppose you create a lambda that accepts a list and prints it as follows:

greet = lambda friends: for friend in friends: print(f"Hello, {friend}!")

You’ll have an invalid syntax error. To print a list, you need to use a list comprehension in the lambda function as follows:

greet = lambda friends: [print(f"Hello, {friend}!") for friend in friends]

greet(["John", "Jane", "Lisa"])

# Hello, John
# Hello, Jane
# Hello, Lisa

However, I think the syntax is confusing because lambda is intended for simple, short functions.

Unless you have to create the function in one line, writing a regular function with the def keyword is preferable.

Conclusion

In Python, you can create a one line function by defining the function body next to the header or using a lambda function. Both methods are severely limited as you can’t include a for or if statement in the function.

One liner functions are good only for the simplest functions because the line breaks and indents in Python are designed to make your code clear and easy to comprehend.

I hope this tutorial is helpful. I’ve also written additional tutorials on Python one liners:

Python one liner for loop
Python one line if without else

And that’s it for this tutorial. Until next time! 🙏

Take your skills to the next level ⚡️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.