. Advertisement .
..3..
. Advertisement .
..4..
Hello everyone! Today we start with an interesting topic. It was “typeerror: list indices must be integers or slices, not tuple” Let’s see how we found the answers below.
Why does this error happen?
You’re going to create a program that collects data about some UK birds. To begin, define a list of birds:
birds = [
["Barn Owl", "Owl", "Green"]
["Black tern", "Gulls and terns", "Green"]
]
Each list starts with the name of a bird, then moves on to its family, then finishes with its UK conservation status. You then request that a user add a record to your list. The input() method is used to accomplish this:
name = input("Enter the name of the bird: ")
family = input("Enter the family of the bird: ")
conservation_status = input("Enter the conservation status of the bird: ")
With this information, you can add a bird’s details to your list of bird species. The add() technique is used to accomplish this:
birds.append([name, family, conservation_status])
print(birds)
After the record you want to add has been added to the list, your code writes out your updated list of birds. Let’s execute your code and observe the results:
Traceback (most recent call last):
File "main.py", line 2, in <module>
["Barn Owl", "Owl", "Green"]
TypeError: list indices must be integers or slices, not tuple
Your program returns an error. Just before you are requested to enter information about a bird, you will notice your mistake. You can see from our stack trace that the issue is on line two of your code, where your list declaration is located.
Solutions for the “typeerror: list indices must be integers or slices, not tuple” error
Option 1: Separate the lists
Your code has a flaw since you neglected to use commas to separate the values in your list. Without commas, your list sees the second record as an index value for the first record, which creates a problem:
birds = [
["Barn Owl", "Owl", "Green"] ["Black tern", "Gulls and terns", "Green"] ]
This code is read as the following by Python:
["Barn Owl", "Owl", "Green"]["Black tern", "Gulls and terns", "Green"]
A value for the first list’s index cannot come from the second list. Numbers are required for index values. Because your second list is truncated and includes many values, your code claims that you have specified a tuple.
To fix this error, you must use a comma to separate the lists in your list of lists as the following:
birds = [
["Barn Owl", "Owl", "Green"],
["Black tern", "Gulls and terns", "Green"]
]
Use this with the comma in the birds list here:
Enter the name of the bird: Pied Wagtail
Enter the family of the bird: Pipits and wagtails
Enter the conservation status of the bird: Green
[['Barn Owl', 'Owl', 'Green'], ['Black tern', 'Gulls and terns', 'Green'], ['Pied Wagtail', 'Pipits and wagtails', 'Green']]
Successful code execution occurs. You are required to enter data regarding a bird first. The information is subsequently included in your list of birds by your code. Finally, your code will print out the list of birds. You can see that the list’s final entry includes your new record now.
Option 2: Use colon
A colon (:) plays an important role in Python. In Python, a colon is used for many purposes, including declaring functions, obtaining data, and indexing arrays and more.
To fix the error “typeerror: list indices must be integers or slices, not tuple”, you also can use a proper index and use colon as follow:
# Python Accessing List
numbers=[1,2,3,4]
print(numbers[0:3])
Output
[1, 2, 3]
What is Tuple in Python?
Tuples are the primary data structure used in Python. They allow you to keep an organized series of objects. For instance, you could utilize a tuple for storing an employee’s names. A tuple could be used to keep track of the different flavors of ice cream available at an ice cream store.
In this article we will explain the fundamentals of the tuple type data. We’ll go over its function and give examples of how to work with this type of data.
Further information can be explore more via A Step-By-Step Guide in Python Tuples
An example of Python Tuples
A tuple is a comma separated series of objects. The sequence is enclosed with the parenthesis (()). Let’s build a tuple.
ice_cream_flavors = ('Mango', 'Vanilla', 'Mint', 'Strawberry', 'Chocolate')
If printing our tuple on the console by using print() print() function it will display the tuple we initially declared. The values of our tuple are separated with commas:
print(tuple)
Then, the command will return as follow:
('Mango', 'Vanilla', 'Mint', 'Strawberry', 'Chocolate')
Tuples store information with an underlying design, much like lists. In the case above our tuple holds an assortment of ice cream flavors. It could also contain the grades of students or an inventory of phones offered by an electronics retailer, for instance. [/su_note]
Conclusion
This is how you are able to solve this type of task “typeerror: list indices must be integers or slices, not tuple”. However, we have provided relevant expertise to develop a more research-oriented mindset, and make it easier for you to answer these type questions. Please leave a message below if you have any questions or comments. Thanks for reading!
Read more
→ How to fix “TypeError: ‘numpy.ndarray’ object is not callable in Python”
Leave a comment