Nameerror: name ‘List’ is not defined ( Solved )

Are you getting the Nameerror: name ‘List’ is not defined error? If yes then this post is for you. In this tutorial, you will know why this error comes and how to solve this error quickly.

What is NameError?

NameError is an error that occurs when you are trying to use the variable or function that you have not defined in the code. For example, if you are getting the error Nameerror: name ‘X’ is not defined, then it’s clear that the Python interpreter is telling you that if you have not imported the module function or defined the variable before using the X.

List in python
List in python

What causes Nameerror: name ‘List’ is not defined?

The main reason you are getting the error is that you have not properly used the List constructor in your code. The error will come when you will run the below lines of code.

def f1(self, x: List[int]) -> int:


Nameerror: name 'List' is not defined

Solve name ‘List’ is not defined

The solution to the above error is very simple. Here you have to import the List constructor from the typing module. If you are using the Python version less than 3.9 then you have to first import the List from the typing module before using it in the above function.

Now you will not get the error when you run the below lines of code.

from typing import List
def f1(self, x: List[int]) -> int:

The other solution to overcome this error is to update the Python version. The Python version greater than 3.9 does not require importing List from typing module. Instead, you can use the list to define the array.

You can use the below lines of code.

def f1(self, x: list[int]) -> int:

You can see instead of the List[int] I am using the list[int].


In most cases the NameError is raised when you are using the variable, function, or constructor without importing or defining. If you are getting the Nameerror: name ‘List’ is not defined error then the above solution will solve this error.

I hope you have liked this tutorial. If you have any queries then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner