The Python nameerror: name ‘list’ is not defined error message can be frustrating if you don’t know how to fix it.
Luckily, in this article, we’ll discuss the solutions for nameerror name list is not defined and nameerror: name ‘List’ is not defined error message.
Aside from that, you’ll also learn what this error means and the reason behind this error.
What is “nameerror name ‘list’ is not defined”?
The nameerror: name ‘list’ is not defined error message that occurs when the Python interpreter cannot find a variable or function with the name list.
In simple words, this error occurs when you’re trying to access a list that is not defined in your Python script.
Why does this error occur?
This error can occur because of some reason, such as:
❌ If you have accidentally misspelled the name of the built-in list type.
❌ If you forget to import the module that contains the list.
❌ If you have accidentally overwritten the built-in list type with another variable or function.
Solutions to fix nameerror name ‘list’ is not defined
The following are the solutions that you can use to fix the error.
1. Verify if the list is defined
The first thing you should do when you encounter this error is to verify if the list is defined in your code.
If not, you need to define the list before you can use it.
2. Double check the spelling of the list name
If you misspelled the name of the list, you must correct it. Python is case-sensitive, ensure that the spelling of the list is correct.
In Python, the built-in list type is spelled with a lowercase
3. Verify the scope of the list
When the list is defined in a different scope than where it is being used, you have to move the definition of the list to the appropriate scope.
For example, if the list is defined inside a function, you cannot use it outside the function unless you return the list from the function.
What is “nameerror: name ‘List’ is not defined”?
The namerror: name ‘List’ is not defined error message that occurs when you’re trying to use the List type from the typing module in Python.
However, you did not import it correctly.
How to fix nameerror: name ‘List’ is not defined?
To fix the error, you can try adding from typing import List at the beginning of your code.
1. Add Import List at the beginning you your code
This will import the List type from the typing module in Python and should resolve the issue
For example:
from typing import List
def sample_function(sample_list: List[int]) -> int:
total = 0
for item in sample_list:
total += item
return total
result = sample_function([1, 2, 3, 4, 5])
print(result)
As you can see, the List type is imported from the typing module at the beginning of the code.
And the sample_function function takes a List of integers as an argument and returns the sum of all the integers in the list.
After that, the code calls the sample_function function with a list of integers as an argument and prints the result.
Output:
15
2. Use built-in collection types
If you’re using Python 3.9 or recent, you can use built-in collection types (like list) as generic types instead of importing the corresponding capitalized types from typing.
Take note: We use list rather than List.
def sample_function(sample_list: list[int]) -> int:
total = 0
for item in sample_list:
total += item
return total
result = sample_function([10, 20, 30, 40, 50])
print(result)
Here, the sample_function function takes a list of integers as an argument and returns the sum of all the integers in the list.
Then calls the sample_function function with a list of integers as an argument and prints the result.
Output:
150
Difference between “nameerror: name ‘list’ is not defined” and “nameerror: name ‘List’ is not defined”
The difference between “List” and “list” is that “List” refers to the typing module’s List type hint, which is used to annotate lists, while ‘list‘ refers to the built-in Python list data type.
In order to fix the error, you have to import the typing module if you are using “List”, or simply use ‘list’ if you intended to use the built-in list data type.
Frequently Asked Questions (FAQs)
How to define a list in Python?
To define a list in Python, you can use the square brackets [] and separate the elements with commas.
For example:
sample_list = [1, 2, 3, 4, 5]
Why am I encountering the “Nameerror: name ‘list’ is not defined” error?
You are encountering the “Nameerror: name ‘list’ is not defined” error because you are trying to access a list that is not defined in your code.
Conclusion
The nameerror: name ‘list’ is not defined error message that occurs when the Python interpreter cannot find a variable or function with the name list.
On the other hand, namerror: name ‘List’ is not defined error message that occurs when you’re trying to use the List type from the typing module in Python.
This article already provides solutions for these two errors to help you fix the Python error message.
You could also check out other “nameerror” articles that may help you in the future if you encounter them.
- Nameerror: name ‘__file__’ is not defined
- Nameerror name pd is not defined
- Nameerror: name unicode is not defined
Hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊