Python language- most common errors ; unhashable type list
A list of the most commonly made mistakes in the Python language, from basic to advanced.
This article will help you avoid these mistakes, whether you are a beginner or an experienced Python programmer.
This list is open for community editing, so please feel free to make edits yourself as well.
Note: This is likely not the most comprehensive list possible. There are a lot more ways to go wrong in Python than this list can cover.
We have just tried to think of the most common ones that I have personally run into when teaching Python to others and attempting to learn it myself. Hopefully it will help someone- newbie or old pro alike!
Most common error types of python language :
1.Assignment Loss
Assignment error python : x = y // Python 3
This is a very simple mistake to make when typing on your keyboard. It’s very easy to think that code looks like it works and so you hit the enter key (or whatever you use for your keyboard).
When typing on your keyboard, you often think of yourself as the error handler, which is true. But the computer sees everything as an operator.
So when Python gets executed, it assigns all values in your program to a variable before executing them. In the case of this program above, because x is assigned to y and not y to x , y will end up being equal to 0 . Not very useful.
Fix:
x = y // Python 2 x = y # Python 3
2.Undefined variable error – Uninitialized NameError.
This is one of the most common errors in any language and especially in Python when working with functions. In Python, variables are considered to be implicitly initialized to None .
Which means that if you use a variable before you’ve assigned it a value, you will get an error message saying that it’s undefined.
And since the word None is not the most helpful, this makes it even more confusing for beginners.
3.Assignment Error to List
List Python error : x = [ 0 , 1 , 2 ] // Python 2 x = [ 0 , 1 , 2 ] # Python 3
This is another very simple beginner’s error. It’s easy to forget that lists start at 0 and not 1. So this (in the case of a one item list) assigns the value of 0 to x . But then, when it tries to add the second value of 1 into this, since there is no place for it, it gives you an error. Not very helpful but still indicates that you have a problem somewhere in your code.
4.KeyError and NameError
KeyError : list not defined, NameError: global name ‘var’ is not defined
These two errors are very similar in that they both indicate that you have a problem with referencing a variable somewhere in your program.
But the difference between the two is that KeyErrors happen when you reference a key for an item in a mutable object like a list or dictionary.
But NameErrors indicate that you’ve referenced a name for which there’s simply no definition anywhere to be found.
Note: If it’s only happening in one function, and it seems to be an error with importing something, then it might just be an issue with your import statement.
5. Unhashable type list :
Python lists are unhashable. This is because they have no mutable methods, only their values can be changed.
Since the elements cannot be changed, even if you try to change them, they will at most return an error message like:
List not defined: ‘l’, ‘l’ in Python 3 example : x = [ 0 , 1 , 2 ] # Python 3 < x is not defined >
This usually happens because you are trying to change the value of a list or dictionary inside it’s class definition and you’re using a list object instead of just using the list data structure that it’s made up of.
6.Enum type error :
Enum Python Example : x = enum_class # Python 3 x is not defined
This usually happens when you try to use something which is not actually an object in Python.
This error might happen because you are attempting to assign to a variable of type Enum instead of trying to use it as a list or dictionary as it was originally intended.
The solution here could be getting rid of the statement and assigning the result directly into the variable, since lists and dictionaries already have a built-in thing that will let you treat them like an enumeration with their values associated with each member.