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
  • TypeError: not all arguments converted during string formatting Python
  • What does a data analyst do? Livestream
  • How to Pass Python Variables to Javascript Javascript
  • How to import data into excel Python Tutorial
  • YouTube channel lists – Python Lists Python Lists
  • Create a HTML Table From Python Using Javascript Javascript
  • How can packages be managed in Julia? Julia programming
  • How to Generate Random Integers Between 0 and 9 Python

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

  • how to remove spaces from a string regular expressions
  • TypeError: not all arguments converted during string formatting Python
  • TypeError: cannot unpack non-iterable int object Python
  • TypeError: the first argument must be callable Python
  • String Manipulation in Python python method
  • How do I fix TypeError: unhashable type: ‘list’ Error? Articles

Select your language!

  • हिंदी
  • Español
  • Português
  • Français
  • Italiano
  • Deutsch
  • how to add sine and cosine in python code numpy
  • What is an Algorithm? machine learning
  • How to create a calculated field in Tableau data visualisation
  • How to Group By in a Select Statement SQL
  • How to Create an XML file from Excel using Python Python
  • How to count the no of rows and columns in a CSV file CSV
  • Import a CSV file with an SQL query CSV
  • How to check if a file is empty Python

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