1 / 12

Python - TypeError – NoneType Object not Subscriptable

https://360digitmg.com/python-typeerror-nonetype-object-is-not-subsriptable

DigiTMG
Télécharger la présentation

Python - TypeError – NoneType Object not Subscriptable

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 07/09/2020 Python “TypeError – NoneType Object not Subscriptable" - 360DigiTMG Spam Score: PA: 28 0 links DA: 36 12% Home (https://360digitmg.com/) / Blog (https://360digitmg.com/blog) / Data Science (https://360digitmg.com/blog-category/data-science) / Python - TypeError – NoneType Object not Subscriptable Python - TypeError – NoneType Object not Subscriptable by Mr. Nikhil Miryala September 05, 2020  258 Error!! Error!!! Error!!!! These are common popups we get while executing code, we might feel panic or irritated when we see such errors. But error noti?cations are actually problem solvers, every error will be identi?ed with the proper error name which helps us to ?x the error in the right direction. A Group of friends is living together due to COVID-19 lockdown, they are working on Python and they got different errors individually and started discussing their errors. So, here goes the discussion between them. Ram: Oops!! I just got an error ☹ What error did you get? : Nithin Ram: I got a None-Type object not subscriptable error! Oh really, I got the same error too in yesterday’s task. : Yash Ram: You were working on a different task but still you got the same error? Strange! Oh, is it? how come the same error for different tasks : Yash So, there is nothing much strange here!  Message us (https:/ /wa.me/919989994319) Request A Callback    They are an absolute change of getting the same error for different cases. I have a few examples lets discuss it. : Nitin Write to us (mailto:enquiry@360digitmg.com) Chat https://360digitmg.com/python-typeerror-nonetype-object-is-not-subsriptable 1/12

  2. 07/09/2020 TypeError: 'NoneType' object is not subscriptable Python “TypeError – NoneType Object not Subscriptable" - 360DigiTMG The error is self-explanatory. You are trying to subscript an object which is a None actually... Example 1 list1=[5,1,2,6]       # Create a simple list order=list1.sort() # sort the elements in created list and store into another variable. order[0]              # Trying to access the ?rst element after sorting TypeError Traceback (most recent call last) in () list1=[5,1,2,6] order=list1.sort() ----> order[0] TypeError: 'NoneType' object is not subscriptable sort () method won't return anything but None. It directly acts upon source object. So, you are trying to slice/subscript the None object which holds no data at all. print(order)  # Let’s see the data present in the order variable. None      # It’s None REMEDY Check for the returned value whether it is None. If it is None, then plan for the alternatives accordingly. In the above example, we are trying to sort the values in a list. when sort() is used, the source object itself gets modi?ed, without returning anything. We can try other ways to get the error resolved. i. either operate on the source directly. list1=[5,1,2,6] #A simple List list1.sort()   # Sort the elements of source object directly. list1[0]       # Access the ?rst element of the sorted list. Output: 1  Message us (https:/ /wa.me/919989994319) Request A Callback    ii. or use another method that returns the object with sorted values of the list. Write to us (mailto:enquiry@360digitmg.com) https://360digitmg.com/python-typeerror-nonetype-object-is-not-subsriptable 2/12

  3. 07/09/2020 Python “TypeError – NoneType Object not Subscriptable" - 360DigiTMG list1=[5,1,2,6]              # A simple list created order=sorted(list1) # Sorting the elements of the above list and store them into another list. order[0]                  # Access ?rst element of a new list created.   Output: 1 Example 2 list1=[1,2,4,6]                           # Create a list with a few elements reverse_order=list1.reverse() # Reverse the order of elements and store into other variable. reverse_order[0:2]                  # Access the ?rst two elements after reversing the order.   TypeError Traceback (most recent call last) in ()      list1=[1,2,4,6]      reverse_order=list1.reverse() ----> reverse_order[0:2] TypeError: 'NoneType' object is not subscriptable the reverse() method also won't return anything but None, it directly acts upon the source object. So you are trying to slice/subscript the None object which holds no data at all. print(reverse_order) # Prints the data inside reverse_order None #It’s None REMEDY Always check for the returned values whether it is None. If it is None, then plan the alternatives accordingly. In the above example, we are trying to reverse the elements of a list. when reverse() is used, the source object itself gets modi?ed, without returning anything. We can try other ways to get the error resolved. Write to us (mailto:enquiry@360digitmg.com)  Message us (https:/ /wa.me/919989994319) Request A Callback    https://360digitmg.com/python-typeerror-nonetype-object-is-not-subsriptable 3/12

  4. 07/09/2020 Python “TypeError – NoneType Object not Subscriptable" - 360DigiTMG i. Either operate on the source directly. list1=[1,2,4,6] #A simple list list1.reverse() # Reverse the order of elements in the above list list1[0:2]         # Accessing the ?rst two elements in a reversed list. Output: [6, 4] ii. Use another method that returns the object with reversed values of the list. list1=[1,2,4,6]                           # A list reverse_order=list(reversed(list1)) # Reversing the order and store into another list. reverse_order[0:2]                         # Accessing the ?rst 2 elements of reversed list. Output: [6, 4] Example 3 Import numpy as np # Import numpy package def addition(arrays):        # A function which performs addition operation total=arrays.sum(axis=1) # Performing row wise addition in numpy array a=np.arange(12).reshape(6,2) #Creating a 2D array total=addition(a)                        #Calling the function total[0:4]                                    #Accessing ?rst 4 elements on total TypeError Traceback (most recent call last) in ()     a=np.arange(12).reshape(6,2)     total=addition(a) ----> total[0:4] TypeError: 'NoneType' object is not subscriptable Here if we observe, the function addition is not returning anything. When we try to subscript the returned object it will give the above error as it holds nothing.  Message us (https:/ /wa.me/919989994319) Request A Callback    print(total) #print total None Write to us (mailto:enquiry@360digitmg.com) https://360digitmg.com/python-typeerror-nonetype-object-is-not-subsriptable 4/12

  5. 07/09/2020 Python “TypeError – NoneType Object not Subscriptable" - 360DigiTMG REMEDY Always check for the returned values whether it is None. If it is None, then plan the alternatives accordingly. In the above example, we are trying to print the sum of the row-wise elements in a 2D array. We can try other ways to get the error resolved. i. Either print the sum inside the function directly. import numpy as np # Import numpy package for mathematical operations defaddition1(arrays):                  # Function to perform addition total1=arrays.sum(axis=1)        # Row-wise sum in 2D numpy array print('row wise sum',total1[0:4]) # Printing ?rst 4 elements of total a=np.arange(12).reshape(6,2) # Creating a 2D array print('input array \n',a)              # Printing a comment print('*********') addition1(a)                            # Calling the function input array [[ 0 1] [ 2 3] [ 4 5] [ 6 7] [ 8 9] [10 11]] ********* row wise sum [ 1 5 9 13] ii. import numpy as np # Import numpy package defaddition2(arrays): # De?ning a function which performs addition return total               # Returning the sum a=np.arange(12).reshape(6,2) # Creating a 2D array print('input array \n',a) print('*********') total2= addition2(a)                     # Calling the function print('row wise sum',total2[0:4]) # Printing ?rst 4 elements of total  Message us (https:/ /wa.me/919989994319) Request A Callback    Write to us (mailto:enquiry@360digitmg.com) https://360digitmg.com/python-typeerror-nonetype-object-is-not-subsriptable 5/12

  6. 07/09/2020 Python “TypeError – NoneType Object not Subscriptable" - 360DigiTMG input array [[ 0 1] [ 2 3] [ 4 5] [ 6 7] [ 8 9] [10 11]] ********* row wise sum [ 1 5 9 13] Example4 import cv2                                         # Importing opencv package to read the image import numpy as np                          # Importing numpy package import matplotlib.pyplot as plt           # Package for visualization image=cv2.imread('download.jpeg')    # Reading the image plt.imshow(image[65:120])              # Show the part of image TypeError Traceback (most recent call last) in ()     importmatplotlib.pyplotasplt     image=cv2.imread('download.jpeg') ----> plt.imshow(image[65:120]) TypeError: 'NoneType' object is not subscriptable Here we are trying to print a cut short of the image. Above error which is coming as OpenCV is unable to read the image print(image) None REMEDY Look for the correct path and correct the format of the image. If the path is wrong or the image type is wrong then, OpenCV won't able to load the image. Here the image type is 'jpg' not 'jpeg'  Message us (https:/ /wa.me/919989994319) Request A Callback    Write to us (mailto:enquiry@360digitmg.com) https://360digitmg.com/python-typeerror-nonetype-object-is-not-subsriptable 6/12

  7. 07/09/2020 Python “TypeError – NoneType Object not Subscriptable" - 360DigiTMG import cv2                               # Import opencv package to read the image import numpy as np                       # Numpy package import matplotlib.pyplot as plt         # For visualization image=cv2.imread('download.jpg') # Reading the image plt.imshow(image[65:120])             # Showing the part of image Output: Conclusion: The TypeError is a common error message we get while we are computing different data types which are not compatible. Whenever we are trying to subscript/slice the none type data(object/value) or empty data object then we will get the TypeError: None-type not subscriptable error.  (https://www.linkedin.com/company/360-                          (https://www.facebook.com/360Digitmg/) (https://twitter.com/360digitmg) digitmg) (https://www.youtube.com/c/360DigiTMG) Category Data Science  Project Management  Tableau  Arti?cial Intelligence  Cloud Computing  Domain Analytics   Message us (https:/ /wa.me/919989994319) Request A Callback    Digital Marketing  Write to us (mailto:enquiry@360digitmg.com) https://360digitmg.com/python-typeerror-nonetype-object-is-not-subsriptable 7/12

  8. 07/09/2020 Python “TypeError – NoneType Object not Subscriptable" - 360DigiTMG Internet Of Things  Interview Questions  HRDF  Software Development  Recent Posts Cloud Computing in Simple Words (https://360digitmg.com/cloud-computing-in-simple- words) Quality Management (https://360digitmg.com/quality-management) A Glimpse of the Industrial Revolution 4.0 (https://360digitmg.com/industrial-revolution-ir- 4.0) Software Development (https://360digitmg.com/software-development) Python - TypeError – NoneType Object not Subscriptable (https://360digitmg.com/python- typeerror-nonetype-object-is-not-subsriptable) You may also like...  Message us (https:/ /wa.me/919989994319) Request A Callback    (https://360digitmg.com/database-administrator-roles) Write to us (mailto:enquiry@360digitmg.com) https://360digitmg.com/python-typeerror-nonetype-object-is-not-subsriptable 8/12

  9. 07/09/2020 Python “TypeError – NoneType Object not Subscriptable" - 360DigiTMG Database Administrator Roles (https://360digitmg.com/database-administrator-roles) September 02, 2020 Communication has been extremely critical to human evolution. The evolution of communication from the ancient period to modern times has been a very interesting metamorphosis. Every generation had a unique way of communicating and sharing knowledge. (https://360digitmg.com/what-is-the-difference-between-analysis-and-analytics) What is the Difference between Analysis and Analytics (https://360digitmg.com/what-is-the-difference-between-analysis-and- analytics) August 25, 2020 Analysis and Analytics sound like Siamese twins. In a way, they are conjoined yet very dissimilar. Due to the similarity in the two terms the way the two words are spelt and pronounced, we easily fall prey by assuming them to mean the same and use it interchangeably.  Message us (https:/ /wa.me/919989994319) Request A Callback    Write to us (mailto:enquiry@360digitmg.com) https://360digitmg.com/python-typeerror-nonetype-object-is-not-subsriptable 9/12

  10. 07/09/2020 (https://360digitmg.com/how-malaysia-is-nurturing-data-scientist-for-2020-and-beyond) Python “TypeError – NoneType Object not Subscriptable" - 360DigiTMG How Malaysia is Nurturing Data Scientist for 2020 and Beyond? (https://360digitmg.com/how-malaysia-is-nurturing-data-scientist-for-2020- and-beyond) August 07, 2020 Data Science has become a leading ?eld of study in recent times owing to its vast use in almost every industry in all parts of the world. Workshops (https://360digitmg.com/workshops) Webinars (https://360digitmg.com/webinars) Corporate Training (https://360digitmg.com/corporate-training) Contact Us (https://360digitmg.com/contact) Privacy Policy (https://360digitmg.com/privacy) India (https://360digitmg.com/) Contact  360DigiTMG - Data Analytics, Data Science Course Training Hyderabad 2-56/2/19, 3rd ?oor, Vijaya Towers, near Meridian School, Ayyappa Society Rd, Madhapur, Hyderabad, Telangana 500081.  enquiry@360digitmg.com (mailto:enquiry@360digitmg.com)  +91-9989994319 (tel:+919989994319) / 1800-212-654321 (tel:1800-212-654321) Follow Us!  (https://www.linkedin.com/company/360-   Message us (https:/ /wa.me/919989994319) Request A Callback      (https://www.facebook.com/360Digitmg/) (https://twitter.com/360digitmg) digitmg) (https://www.youtube.com/c/360DigiTMG) Write to us (mailto:enquiry@360digitmg.com) https://360digitmg.com/python-typeerror-nonetype-object-is-not-subsriptable 10/12

  11. 07/09/2020 Python “TypeError – NoneType Object not Subscriptable" - 360DigiTMG Emerging Technologies Data Science (Https://360digitmg.Com/India/Exclusive-Data-Science-Program-For-  Students) Digital Marketing (Https://360digitmg.Com/India/Advanced-Program-In-Digital-  Marketing-Using-Arti?cial-Intelligence) IR 4.0 (Https://360digitmg.Com/India/Certi?cation-On-Industrial-Revolution-4-0)  PMP (Https://360digitmg.Com/India/Project-Management-Professional-Pmp)  Arti?cial Intelligence (Https://360digitmg.Com/India/Arti?cial-Intelligence-Ai-And-Deep-  Learning) Big Data Analytics :  Data Science Training in India  | (https://360digitmg.com/india/exclusive-data-science-program-for- students) AI Training in India  | (https://360digitmg.com/india/arti?cial-intelligence-ai-and-deep-learning) Data Visualization Training in India  | (https://360digitmg.com/india/data-visualization-using-tableau) Big Data Training in India  | (https://360digitmg.com/india/big-data-for-managers) Project Management :  PMP Training in India  | (https://360digitmg.com/india/project-management-professional-pmp) Digital Marketing :  Advanced Program in Digital Marketing Training in India  | (https://360digitmg.com/india/advanced- program-in-digital-marketing-using-arti?cial-intelligence)  Message us (https:/ /wa.me/919989994319) Request A Callback    Write to us (mailto:enquiry@360digitmg.com) https://360digitmg.com/python-typeerror-nonetype-object-is-not-subsriptable 11/12

  12. 07/09/2020 Python “TypeError – NoneType Object not Subscriptable" - 360DigiTMG Domain Analytics : Life Sciences and HealthCare Analytics Program  | (https://360digitmg.com/india/life-sciences-and- healthcare-analytics-certi?cation-programme) Certi?cation Program in Financial Analytics  | (https://360digitmg.com/india/certi?cation-program-in- ?nancial-analytics) Certi?cation Program in Marketing Analytics  | (https://360digitmg.com/india/certi?cation-program-in- marketing-analytics) HR Analytics Certi?cation Program  | (https://360digitmg.com/india/hr-analytics-certi?cation-program) Certi?cation Program in Supply Chain Analytics  | (https://360digitmg.com/india/certi?cation-program-in- supply-chain-analytics) Certi?cation Program in Cyber Security Analytics  | (https://360digitmg.com/india/certi?cation-program- in-cyber-security-analytics) Data Science for Internal Auditors  | (https://360digitmg.com/india/data-science-for-internal-auditors) Copyright © 2020 360DigiTMG (https://360digitmg.com/). All rights reserved.  Message us (https:/ /wa.me/919989994319) Request A Callback    Write to us (mailto:enquiry@360digitmg.com) https://360digitmg.com/python-typeerror-nonetype-object-is-not-subsriptable 12/12

More Related