470,575 Members | 1,936 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,575 developers. It's quick & easy.

TypeError: 'type' object is not subscriptable

1 Bit
Expand|Select|Wrap|Line Numbers
  1. def merge_and_swap(list1, list2):
  2.   list1=list1+list2
  3.  
  4.   list1.insert(0,list[-1])
  5.   list1.pop(4)
  6.  
  7.   list1.insert(1,list[4])
  8.   list1.pop(1)
  9.  
  10.   return list1
  11.  
  12. print(merge_and_swap([1,2,3], [5]))
  13.  
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.
Jan 23 '21 #1
3 5844
Rabbit
12,516 Expert Mod 8TB
You reference a non-existent list, you probably meant list2
Jan 23 '21 #2
Sage11
1 Bit
The issue is you typed
Expand|Select|Wrap|Line Numbers
  1. list[-1]
. list is a type. I think you mean
Expand|Select|Wrap|Line Numbers
  1. list1[-1]
Jan 31 '21 #3
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.
Dec 14 '21 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Atul Kshirsagar | last post: by
2 posts views Thread by Balaji | last post: by
5 posts views Thread by Randall Parker | last post: by
9 posts views Thread by k.retheesh | last post: by
10 posts views Thread by Charles Russell | last post: by
33 posts views Thread by christophertidy | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.