Tuple Indices Must Be Integers Or Slices, Not Str | How To Fix Typeerror: List Indices Must Be Integers Or Slices, Not ‘str’?

Finxter - Create Your Coding Business

Subscribe Here





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


Hey, fingster’s 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 iterate’s 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, lets. 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 lets. 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.

0.3.0 | Wor Build 0.3.0 Installation Guide

Transcript: [MUSIC] Okay, so in this video? I want to take a look at the new windows on Raspberry Pi build 0.3.0 and this is the latest version. It's just been released today and this version you have to build by yourself. You have to get your own whim, and then you...

read more