Python throws error, ‘type’ object is not subscriptable, when we try to index or subscript an element of type type
.
Consider this code –
print(list[0]) # TypeError: 'type' object is not subscriptable
The problem with this code is that list
is a built-in function which converts a string into characters array. Here we are subscripting the list
function as if it was a list
array.
You may use this –
list = list("Captain America") print(list[0])
This is perfectly valid but it should never be used. Never use built-in function names as variable names. Why? Check this post – ‘list’ object is not callable.
The correct way is this –
newList = list("Captain America") print(newList[0])
According to Python documentation, there are many built-in types –
- Boolean – bool
- Numeric – int, float, complex
- Iterator
- Sequence – list, tuple, range
- Text – str
- Binary – bytes, bytearray, memoryview
- Set – set, frozenset
- Mapping – dict
- Others
Built-in functions list
These are the built-in functions which will return either type
or builtin_function_or_method
as their type –
abs() | dict() | help() | min() | setattr() |
all() | dir() | hex() | next() | slice() |
any() | divmod() | id() | object() | sorted() |
ascii() | enumerate() | input() | oct() | staticmethod() |
bin() | eval() | int() | open() | str() |
bool() | exec() | isinstance() | ord() | sum() |
bytearray() | filter() | issubclass() | pow() | super() |
bytes() | float() | iter() | print() | tuple() |
callable() | format() | len() | property() | type() |
chr() | frozenset() | list() | range() | vars() |
classmethod() | getattr() | locals() | repr() | zip() |
compile() | globals() | map() | reversed() | __import__() |
complex() | hasattr() | max() | round() | |
delattr() | hash() | memoryview() | set() |
Functions with type as 'type'
Functions in the below list will return 'type'
as their type and indexing or subscripting them will throw the error, ‘type’ object is not subscriptable. So check if you have used any of these functions in your code and subscripting them –
- dict
- slice
- object
- enumerate
- staticmethod
- int
- str
- bool
- bytearray
- filter
- super
- bytes
- float
- tuple
- property
- type
- frozenset
- list
- range
- classmethod
- zip
- map
- reversed
- complex
- memoryview
- set
This code example will show the type of all built-in functions –
print(type(abs)) print(type(dict)) print(type(help)) print(type(min)) print(type(setattr)) print(type(all)) print(type(dir)) print(type(hex)) print(type(next)) print(type(slice)) print(type(any)) print(type(divmod)) print(type(id)) print(type(object)) print(type(sorted)) print(type(ascii)) print(type(enumerate)) print(type(input)) print(type(oct)) print(type(staticmethod)) print(type(bin)) print(type(eval)) print(type(int)) print(type(open)) print(type(str)) print(type(bool)) print(type(exec)) print(type(isinstance)) print(type(ord)) print(type(sum)) print(type(bytearray)) print(type(filter)) print(type(issubclass)) print(type(pow)) print(type(super)) print(type(bytes)) print(type(float)) print(type(iter)) print(type(print)) print(type(tuple)) print(type(callable)) print(type(format)) print(type(len)) print(type(property)) print(type(type)) print(type(chr)) print(type(frozenset)) print(type(list)) print(type(range)) print(type(vars)) print(type(classmethod)) print(type(getattr)) print(type(locals)) print(type(repr)) print(type(zip)) print(type(compile)) print(type(globals)) print(type(map)) print(type(reversed)) print(type(__import__)) print(type(complex)) print(type(hasattr)) print(type(max)) print(type(round)) print(type(delattr)) print(type(hash)) print(type(memoryview)) print(type(set))