Select Page

How to Solve Python SyntaxError: positional argument follows keyword argument

by | Programming, Python, Tips

There are two types of arguments in Python: keyword and positional arguments. The syntax of Python requires that these types of arguments appear in a particular order. If you place a positional argument after a keyword argument, the Python interpreter will throw the error “SyntaxError: positional argument follows keyword argument”.

To solve this problem, ensure that you specify the key for all arguments or ensure the keyword arguments come after the positional arguments.

This tutorial will go through the error in detail and an example to learn how to solve it.


SyntaxError: positional argument follows keyword argument

What is a SyntaxError in Python?

The term syntax refers to the rules that define the various combinations of symbols to instruct a computer to perform tasks. A syntax error violates the syntax rules for a given programming language, and a syntax error is similar to a grammatical error in human languages.

When you run a Python program, the interpreter will parse it to convert the code into Python byte code to execute it. If there is invalid syntax in the Python code during the parsing stage of execution, the interpreter will throw a SyntaxError.

For further reading on SyntaxError involving arguments, go to the article: How to Solve Python SyntaxError: non-default argument follows default argument.

Keyword and Positional Arguments in Python

There are two types of arguments for functions in Python, keyword and positional. We identify a keyword argument by a key, whereas we identify a positional argument by its position in the function definition. Let’s look at an example.

def test_func(x, y, z = 5):
    
    print('x = ', x)
    
    print('y = ', y)
    
    print('z = ', z)

print('Example Function call 1')

test_func(2, 4, 6)

print('Example Function call 2')

test_func(2, 4)

print('Example Function call 3')

test_func(z=10, x=3, y=2)

In the above program, we have a function called test_func that takes three arguments. We then perform three function calls with different positional and keyword arguments.

Let’s run all three function calls to get the result:

Example Function call 1
x =  2
y =  4
z =  6
Example Function call 2
x =  2
y =  4
z =  5
Example Function call 3
x =  3
y =  2
z =  10
  • In the first function call, we provide three positional arguments. In this case, the order of the arguments must match the order the function expects them.
  • In the second function call we provide only two positional arguments. The code still works because the final argument z is a keyword argument with a default value of 5. Therefore, if we do not specify a different keyword argument the function uses the default value.
  • In third function call, we provide three keyword arguments. In this case the order of the arguments is different from what the function has as default. If you are passing all keyword arguments you do not need to be specific in the positions of the arguments.

Example

Let’s consider an example program that will throw the SyntaxError. We will write a program that creates a dictionary of player names and scores for a game.

players = ["Jane", "Wilson", "Rupert"]

scores = [4, 8, 7]

def scoring_func(players, scores):

    score_dict = dict(zip(players, scores))

    print(score_dict)

In the code, we define two lists, one for player names and one for the scores of each player. The function scoring_func() takes the two lists as positional arguments and uses dict() and zip() to convert the two lists into a dictionary. The function then prints the result to the console. Let’s call the function:

scoring_func(players=players, scores)
    scoring_func(players=players, scores)
                                  ^
SyntaxError: positional argument follows keyword argument

The code throws the SyntaxError because the positional argument scores came after the keyword argument players.

Solution

We can solve this problem in three ways. Firstly we can make all of the arguments positional. Let’s look at the revised code:

players = ["Jane", "Wilson", "Rupert"]

scores = [4, 8, 7]

def scoring_func(players, scores):

    score_dict = dict(zip(players, scores))

    print(score_dict)

scoring_func(players, scores)

When we perform the function call, we pass two positional arguments. Let’s run the code to see what happens:

{'Jane': 4, 'Wilson': 8, 'Rupert': 7}

For the second option, if we want to use a keyword, we must ensure no positional arguments follow a keyword argument. Therefore scores can be a keyword argument. Let’s look at the revised code:

players = ["Jane", "Wilson", "Rupert"]

scores = [4, 8, 7]

def scoring_func(players, scores):

    score_dict = dict(zip(players, scores))

    print(score_dict)

scoring_func(players, scores=scores)

When we perform the function call, we pass one positional argument, players and one keyword argument, scores. Let’s run the code to see what happens:

{'Jane': 4, 'Wilson': 8, 'Rupert': 7}

Lastly, for the third option, we can use all keyword arguments. In this case, we can use any order for our arguments. Let’s look at the revised code:

players = ["Jane", "Wilson", "Rupert"]

scores = [4, 8, 7]

def scoring_func(players, scores):

    score_dict = dict(zip(players, scores))

    print(score_dict)

scoring_func(players=players, scores=scores)

We pass two keyword arguments to the function when we perform the function call. Let’s run the code to get the result:

{'Jane': 4, 'Wilson': 8, 'Rupert': 7}

To summarise the options, go through the following table

ArgumentsAllowed?
All position arguments, e.g. foo(1, 2, 3)Yes
Positional argument(s) followed by keyword argument(s) e.g.
foo(3, y=2, z=4)
foo(3, 2, z=4)
Yes
All keyword arguments e.g.
foo(x=3, y=2, z=4)
Yes
Keyword argument(s) followed by positional argument(s) e.g.
foo(x=3, y=2, 4)
foo(x=3, 2, z=4)
foo(3, y=2, z)
No
Argument configurations allowed/not allowed in Python.

Summary

Congratulations on reading to the end of this tutorial! Positional arguments must appear before keyword arguments. If you do not follow this syntax, you will throw the error “SyntaxError: positional argument follows keyword argument”. If you use all keywords, you can avoid this error and pass the arguments in any order. When passing arguments to functions, keywords may also make your code more readable.

Go to the online courses page on Python to learn more about Python for data science and machine learning.

Have fun and happy researching!