How To Fix TypeError: List Indices Must Be Integers Or Slices, Not ‘Str’?

Hey fingsters now, if you have come across this video, then it’s quite possible that you have encountered the following error: list indices must be integers or slices, not string. Now, let’s go ahead and understand the reason behind the occurrence of such errors. With the help of an example.

Here we have a list t which stores 10 transactions made by you recently and now the user has to input the number of transactions that he wants to check and once he enters that, he will be able to see these transactions!

Now, please note that the purpose of this program is to print the number of transactions.

That is, if the user enters three then 5000, 1500 and 10 000 should be printed on the screen!

So we have a for loop, which iterates until the number of transactions entered by the user, and then we print that transaction with the help of its index within the list!

Now, let’s go ahead and execute this code. Let’s try and check the first three transactions? Oh no, so we have a type error which says: list indices must be integers or slices, not string. Now, in the first place, a type error generally occurs when you try to perform an operation which is not allowed in python! So in this case, what is the exact operation that we tried to perform? that was not allowed. If you look closely you’ll find that the user input is a string, and now, when we use our for loop, we are trying to access a particular value within the list t using its index, but this index is now a string. Since n is a string entered by the user. Now in python, you can access an item within an element of a sequential data type like a string, a tuple or a list using its index, but this index must be an integer remember. This index must be an integer if it is not an integer, you will definitely get a type error because you are trying to perform something which is not allowed? That is, you are trying to access an index using a string value.

Okay? Now we know why we encountered the error. Let’s move on to the solution.

A very easy solution to this would be to accept the user input as an integer.

So to do that we are going to use the int function and then accept the user input in the form of an integer. So let’s go ahead and quickly do that: okay and then there’s one more change that we have to make. We have to iterate over the values in the list t ranging from the 0th index to the index entered by the user? So to do that, we need to use the range function, and now this should resolve our issue.

So let’s go ahead and check this: let’s try and check five transactions and there we go. We have successfully resolved the error now, there’s another way. You can do this. If, instead of accepting the user input as an integer, you want to accept it as a string. Then you can make a slight change and that will work as well. Now, instead of iterating over the string with the help of the string input entered by the user, you can typecast it to an integer using the int function within the range function itself, and this should work as well for us. So let’s go ahead and execute this now? Let’s say we want the first three transactions and there we go. This worked as well now. This was a very simple example, and the aim of this example was to make you understand why we get the type error and how to resolve it?

In a simple scenario: now, let’s go ahead and have a look at another example which leads to the occurrence of type error list indices must be integers or slices, not strings. So let’s say we have a list consisting of three dictionaries within it, and now each dictionary has the following keys, which are name phone and address now, once the user inputs, the search term, which happens to be the name of the person or the name of a particular person within a particular dictionary, then our program checks! If that person is present within a dictionary in that list and then it prints the address corresponding to that person, so in order to execute this logic, we have used a for loop, which iterates over the list person and now it searches if the name entered by the user is present within the list.

If so, then it prints the name as well as the address, and if the name is not present, then it prints an invalid entry. Now, as you can see that since i am executing this code in pycharm, hence the areas that lead to the occurrence of an error have already been highlighted by pycharm itself? Now that’s the beauty of pycharm. If you are interested in learning about pycharm and how it works, then we do have an amazing course on pycharm? Please go ahead and have a look at that? The link to that course will be provided in the description to this article? I’ll also provide the link of the article in the description below now? Let’s get back to this question: let’s try and execute this code and let’s see what happens so, it says enter the name of the person to find his address. Suppose we want to find the address of ben okay, so we once again have this error which says list indices must be integers or slices, not strings, and now. Why did we get this error in this scenario? if you have a look once again, the user input is a string, but now you might be wondering that the name has to be a string. Also, the name inside the dictionary is also a string. So why did we get this error? thus the error doesn’t lie in this line of code!

It’s actually this line that is line number 18 where the error begins? Here we are iterating over the list person. Now you might be wondering again that we can definitely iterate over a list the way we have done in this for loop. However, in this case, if you have a look when we are iterating over the list and then we move inside the for loop and then we check if the search term is present inside person, and then we use the key name now in order to access a particular dictionary, you must define which dictionary you are trying to access now to understand this further?

If you have a look at this list person, then this element- or this dictionary is the first element within the list person. So this represents- or this dictionary represents the first or the zeroth index of the list person. Similarly, this dictionary represents the second element of the list person, while this dictionary represents. The third element now consider these dictionaries as single elements. Then, how would you access these elements? you would access them with the help of their indexes and, as i mentioned previously, indexes must be integers. But now, as you move on to this line, that is line number 19 in our code, you’ll find that we are trying to access an element within the list using a string which is name so now. I hope you get the whole idea about this? I understand that this might be little complex for complete beginners. However, i hope that you are getting the idea that you cannot access an index with a string value. It has to be an integer! So how do we make this work so to do that? the first thing that you have to do is to iterate within a range of integer values, so we are going to use the range function and now, within the range function, we have to specify an integer value. Now that integer value is going to be the length of our list person to find the length of a particular list, you can simply use the len function and that will help you to find the length of the list person! Now, if you want to dive deep into built-in functions like length range, then we do have an entire series of videos created by chris, not only videos.

He has created a list of articles and i’m pretty sure that if you have a look and read those articles, you’ll master the built-in functions in python.

Okay?

So now we are iterating within a range of integer values, so we are done with our first step now, as you can see, we still have an error which is suggested by pycharm, and why so? this is because we are still trying to access a certain element within the list with the help of a string value, because name here is a string? So what we basically have to do is first access a particular dictionary now to access a particular dictionary? We have to use square bracket notation and within it we have to specify the index which is given by n in this case, okay and now after accessing that index, which specifies a particular dictionary within the list, we now have to move inside a particular key within that dictionary, which happens to be name, and now this should work for us! So let me repeat it again for you what we did was. We first used our list person and then we accessed the dictionary that we want. That is suppose if we want to find the address of david, so this would be the third dictionary represented by the second index? So that’s the functionality of this square bracket and now, within this dictionary, we want to check if the name david is present! So how do we do that? we do that by accessing the value with the help of its key, which is name.

Similarly, when we are printing the address, we once again have to first access the dictionary and then we can move inside the dictionary to access the key which is address so yet again we use our square bracket notation and then we specify the index, and now this should work for us. So let’s go ahead and execute our code to check if this works.

Okay, so i want to find the address of david and there we go. We have successfully extracted the address of david, so this was the first way of resolving our error. Now, there’s another way that you can do this. Instead of using the range and the len methods, you can use another method known as enumerate now this is an inbuilt method in python which allows you to loop over all elements in an iterable and their associated counters. To dive deep into this method, you can once again go ahead and click on the link to have a look at how the enumerate method works in python. However, in this code, let’s go ahead and implement the enumerate method, which will help us to avoid the type error! So now, instead of using range and len methods, we are going to use the enumerate method and then we will iterate over the list! Person now enumerate not only allows you to loop over the elements of an iterable, but also helps you with a counter to keep track of each iteration.

So in this case, n is our counter, while, let’s say name is the value that we want to extract.

And now, when we move inside this for loop, the value of n helps us to find the index of a particular dictionary, or rather i should say it helps us to find a particular dictionary. And then the value name will help us to find the key that is name within that particular dictionary? So this is how we can avoid using range and the len methods and then replace it with another method known as enumerate. You can use any of these methods, whichever suits you, so let’s go ahead and execute this and let’s check if this works! Now, let’s try and find the address for harry, so let’s say harry and there we go. So that’s the address for harry and we have successfully cast our spell and eliminated the type error?

Okay. That was it for this video. I hope you enjoyed it and you learned something from it. Please stay tuned for more interesting contents.

Goodbye. .