TypeError: 'type' object is not subscriptable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MinkiChung
    New Member
    • Jan 2021
    • 1

    TypeError: 'type' object is not subscriptable

    Code:
    def merge_and_swap(list1, list2):
      list1=list1+list2
    
      list1.insert(0,list[-1])
      list1.pop(4)
    
      list1.insert(1,list[4])
      list1.pop(1)
      
      return list1
      
    print(merge_and_swap([1,2,3], [5]))
    I took a Error line which is 'TypeError: 'type' object is not subscriptable. What's the matter on my code and how can I solve it? Please give me an answer.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You reference a non-existent list, you probably meant list2

    Comment

    • Sage11
      New Member
      • Jan 2021
      • 1

      #3
      The issue is you typed
      Code:
      list[-1]
      . list is a type. I think you mean
      Code:
      list1[-1]

      Comment

      • bennetcole
        New Member
        • Dec 2021
        • 1

        #4
        The error is self-explanatory. You are trying to subscript an object which you think is a list or dict, but actually is None. This means that you tried to do:

        None[something]

        NoneType is the type of the None object which represents a lack of value, for example, a function that does not explicitly return a value will return None . You might have noticed that the method sort() that only modify the list have no return value printed – they return the default None. This is a design principle for all mutable data structures in Python. 'NoneType' object is not subscriptable is the one thrown by python when you use the square bracket notation object[key] where an object doesn't define the __getitem__ method . This is a design principle for all mutable data structures in Python.

        Comment

        Working...