Python Math – TypeError: 'NoneType' object is not subscriptable

Question:

I’m making a small program for math (no particular reason, just kind of wanted to) and I ran into the error “TypeError: ‘NoneType’ object is not subscriptable.

I have never before seen this error, so I have no idea what it means.

import math

print("The format you should consider:")
print str("value 1a")+str(" + ")+str("value 2")+str(" = ")+str("value 3a ")+str("value 4")+str("n")

print("Do not include the letters in the input, it automatically adds them")

v1 = input("Value 1: ")
v2 = input("Value 2: ")
v3 = input("Value 3: ")
v4 = input("Value 4: ")

lista = [v1, v3]
lista = list.sort(lista)

a = lista[1] - lista[0]

list = [v2, v4]
list = list.sort(list)

b = list[1] = list[0]

print str(a)+str("a")+str(" = ")+str(b)

The error:

Traceback (most recent call last):
  File "C:/Users/Nathan/Documents/Python/New thing", line 16, in <module>
    a = lista[1] - lista[0]
TypeError: 'NoneType' object is not subscriptable
Asked By: Fen

||

Answers:

lista = list.sort(lista)

This should be

lista.sort()

The .sort() method is in-place, and returns None. If you want something not in-place, which returns a value, you could use

sorted_list = sorted(lista)

Aside #1: please don’t call your lists list. That clobbers the builtin list type.

Aside #2: I’m not sure what this line is meant to do:

print str("value 1a")+str(" + ")+str("value 2")+str(" = ")+str("value 3a ")+str("value 4")+str("n")

is it simply

print "value 1a + value 2 = value 3a value 4"

? In other words, I don’t know why you’re calling str on things which are already str.

Aside #3: sometimes you use print("something") (Python 3 syntax) and sometimes you use print "something" (Python 2). The latter would give you a SyntaxError in py3, so you must be running 2.*, in which case you probably don’t want to get in the habit or you’ll wind up printing tuples, with extra parentheses. I admit that it’ll work well enough here, because if there’s only one element in the parentheses it’s not interpreted as a tuple, but it looks strange to the pythonic eye..

Answered By: DSM

The exception TypeError: 'NoneType' object is not subscriptable happens because the value of lista is actually None. You can reproduce TypeError that you get in your code if you try this at the Python command line:

None[0]

The reason that lista gets set to None is because the return value of list.sort() is None… it does not return a sorted copy of the original list. Instead, as the documentation points out, the list gets sorted in-place instead of a copy being made (this is for efficiency reasons).

If you do not want to alter the original version you can use

other_list = sorted(lista)
Answered By: aculich

At this link https://docs.python.org/2/tutorial/datastructures.html you can read this method
“Sort the items of the list in place” this means that the result value will on sorted and
the result will be on itself. The function returns None.

When you assign the result to “lista” in line 14

lista = list.sort(lista)

you area setting it to None. That is the error. None always has no data and can not be
subscriptable. “TypeError: ‘NoneType’ object is not subscriptable”

to correct this error (for sort the list) do this at line 14:

lista.sort() # this will sort the list in line

But there are some other errors:
in line 18 when you assign:

list = [v2, v4]

You clob this built in type “list” and you will get the following error:

TypeError: 'list' object is not callable

To correct this do that, say:

lista2 = [v2, v4]

Again in line 19 the same error of line 14. Do this to sort the other list:

lista2.sort()

In line 21 you are trying to index the built in type list. To correct do this:

b = lista2[1] = lista2[0]

With this your code will run fine. Finally the whole correct code:

import math

print("The format you should consider:")
print str("value 1a")+str(" + ")+str("value 2")+str(" = ")+str("value 3a ")+str("value 4")+str("n")

print("Do not include the letters in the input, it automatically adds them")

v1 = input("Value 1: ")
v2 = input("Value 2: ")
v3 = input("Value 3: ")
v4 = input("Value 4: ")

lista = [v1, v3]
lista.sort()

a = lista[1] - lista[0]

lista2 = [v2, v4]
lista2.sort()

b = lista2[1] = lista2[0]

print str(a)+str("a")+str(" = ")+str(b)
Answered By: Antonio Peixoto

As previously said in one of the answers this error occurs when value of the list turns out to be empty. Well though not fully relevant to this question, the same error occured for me while reading images using opencv and numpy because the file name was found to be different than that specified probably or because the working directory has not been specified properly.

Answered By: ashwinjoseph

it should be
lista.sort()

in line 18

Answered By: Aditya Prasad
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.