25
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?

tripleee
  • 175,061
  • 34
  • 275
  • 318
user2786555
  • 299
  • 1
  • 3
  • 5
  • 1
    The mistake that you are making is that you are whitespacing print function call wrong, it should be `print(list[x])[0]` according to PEP8, and your mistake becomes more obvious. ;) – Antti Haapala -- Слава Україні Sep 18 '13 at 08:03
  • 1
    Though the error message is different, this is basically the same problem as [NoneType has no attribute 'something'](/questions/8949252/why-do-i-get-attributeerror-nonetype-object-has-no-attribute-something) – tripleee Nov 06 '20 at 13:52

6 Answers6

21

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.

TerryA
  • 58,805
  • 11
  • 114
  • 143
14

The [0] needs to be inside the ).

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
2

Don't use list as a variable name for it shadows the builtin.

And there is no need to determine the length of the list. Just iterate over it.

def printer(data):
    for element in data:
        print(element[0])

Just an addendum: Looking at the contents of the inner lists I think they might be the wrong data structure. It looks like you want to use a dictionary instead.

Matthias
  • 12,873
  • 6
  • 42
  • 48
2

Point A: Don't use list as a variable name Point B: You don't need the [0] just

print(list[x])
Cam92
  • 21
  • 4
1

The indexing e.g. [0] should occour inside of the print...

keremistan
  • 414
  • 5
  • 17
1
list1 = ["name1", "info1", 10]
list2 = ["name2", "info2", 30]
list3 = ["name3", "info3", 50]

def printer(*lists):
    for _list in lists:
        for ele in _list:
            print(ele, end = ", ")
        print()

printer(list1, list2, list3)
Joshua Nixon
  • 1,379
  • 2
  • 12
  • 25