list1 = ["name1", "info1", 10] list2 = ["name2", "info2", 30] list3 = ["name3", "info3", 50] MASTERLIST = [list1, list2, list3] def printer(lst): print ("Available Lists:") for x in range(len(lst)): print (lst[x])[0]
This code is returning the "'NoneType' object is not subscriptable" error when I try and run
printer(MASTERLIST)
What did I do wrong?
None always has no data and can not be subscriptable. In general, the error means that you attempted to index an object that doesn't have that functionality. You might have noticed that the method sort() that only modify the list have no return value printed – they return the default None.
The “TypeError: 'NoneType' object is not subscriptable” error is raised when you try to access items from a None value using indexing. This is common if you use a built-in method to manipulate a list and assign the result of that method to a variable.
The error “TypeError: 'NoneType' object is not iterable” occurs when you try to iterate over a NoneType object. Objects like list, tuple, and string are iterables, but not None. To solve this error, ensure you assign any values you want to iterate over to an iterable object.
Methods are not subscriptable objects and therefore cannot be accessed like a list with square brackets. To solve this error, replace the square brackets with the round brackets after the method's name when you are calling it.
The print()
function returns None
. You are trying to index None. You can not, because 'NoneType' object is not subscriptable
.
Put the [0]
inside the brackets. Now you're printing everything, and not just the first term.
The [0]
needs to be inside the )
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With