Skip to content
  • YouTube
  • FaceBook
  • Twitter
  • Instagram

Data Analytics Ireland

Data Analytics and Video Tutorials

  • Home
  • Contact
  • About Us
    • Latest
    • Write for us
    • Learn more information about our website
  • Useful Links
  • Glossary
  • All Categories
  • Faq
  • Livestream
  • Toggle search form
  • Python Tutorial: Add a column to a data frame Python Dataframe
  • python classes class
  • How To Create An Empty Dictionary In Python python dictionaries
  • How to pass by reference or pass by value in Python Python
  • How to create a combobox in tkinter Python
  • How to use wildcards in SQL SQL
  • How to use zip() function in Python Python
  • Python Tutorial: How to create charts in Excel Python Tutorial

TypeError: type object is not subscriptable

Posted on January 25, 2021July 30, 2023 By admin

Estimated reading time: 2 minutes

I was recently working on our last blog post how to reverse a string in python and I came across this error.

The thought passed me what does it mean and how can I fix it?

So what does the error actually mean?

Essentially it means that , you are trying to access an object type, that has a property of “type”.

What are typical property of types? Well they can be:

  • int()
  • str()
  • tuple()
  • dict()

The above allows you to change your data to these data types, so the data contained within them can be further manipulated.

In essence, you are trying to call type in the wrong way and in the wrong place in your code.

By calling it, it will throw this error, and they should be avoided, as they are a built in function.

Lets take an example of how we can replicate this error and fix it

name1 = "joe" # These have index values of [0,1,2]
emptylist =[]
strlength = len(name1) # Returns length of three
while strlength > 0:
    emptylist += str[strlength - 1] #This is the last index value of the variable "name1"
    strlength = strlength - 1
print(emptylist)

In the above code all appears well, but in line 5 the “str” before the [ is the problem. The code automatically looks to call this function.

The simple answer to fixing this is to rename it to name1 as follows:

data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">name1 = "joe" # These have index values of [0,1,2]
emptylist =[]
strlength = len(name1) # Returns length of three
while strlength > 0:
    emptylist += name1[strlength - 1] #This is the last index value of the variable "name1"
    strlength = strlength - 1
print(emptylist)

which gives you the following error-free output:

Result with no error: ['e', 'o', 'j']
https://youtu.be/980qFsQZnLk

 

So it is clear that referencing types as a string variable should be avoided, and keep your code clean from this perspective.

This would also apply to any reserved words in Python as well.

Have you seen type error list object is not an iterator?

Also you may have come across type error float object is not callable

 

strings, type error Tags:built in function, not subscriptable, reserved words, string variables, type object, type object is not subscriptable

Post navigation

Previous Post: how to reverse a string in python
Next Post: how to compare two lists in Python

Related Posts

  • TypeError: ‘NoneType’ object is not iterable class
  • TypeError object of type ‘int’ has no len() Python
  • TypeError: not all arguments converted during string formatting Python
  • How to use wildcards in SQL SQL
  • TypeError: ‘int’ object is not callable Python
  • TypeError: Array() Argument 1 Must Be A Unicode Character, Not List array

Select your language!

  • English
  • हिंदी
  • Español
  • Português
  • Français
  • Italiano
  • How to check if a file is empty Python
  • how to select all records with SQL SQL
  • YouTube channel lists – Python DataFrames Python Dataframe
  • ValueError: invalid literal for int() with base 10 Value Error
  • Welcome to Data Analytics Ireland Livestream
  • What is a Unique key in SQL? SQL
  • How to Compare Column Headers in CSV to a List in Python CSV
  • How to remove unwanted characters Python Data Cleansing

Copyright © 2023 Data Analytics Ireland.

Powered by PressBook Premium theme

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT