Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'NoneType' object is not subscriptable?

Tags:

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?

like image 785
user2786555 Avatar asked Sep 18 '13 07:09

user2786555


People also ask

How do you resolve NoneType objects are not Subscriptable?

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.

What does it mean by NoneType object is not Subscriptable?

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.

How do I fix NoneType in Python?

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.

How do you fix method object is not Subscriptable?

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.


2 Answers

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.

like image 85
TerryA Avatar answered Oct 10 '22 17:10

TerryA


The [0] needs to be inside the ).

like image 20
Ethan Furman Avatar answered Oct 10 '22 15:10

Ethan Furman