Python Forum
list indices must be integers or slices, not lists error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list indices must be integers or slices, not lists error
#1
For the following code:
def create_ID(tmp):
    try_again = True
    in_list = False
    while try_again == True:
        New_ID = input("Please enter the new ID")
        y = 0
        for x in tmp:
            if New_ID in tmp[x][0]: # checks if ID is already in list row by row
                print("That ID is already on the list") # if it is return this error message 
                in_list = True
                y = y + 1
        if in_list == False: # if ID is not in the list close the loop
            try_again = False
    return New_ID
I'm getting this error:

Error:
Traceback (most recent call last): File "c:/Users/djwil/Documents/python/learning python/Chapter 19 - Chunky Challenges/Passwords.py", line 173, in <module> main() File "c:/Users/djwil/Documents/python/learning python/Chapter 19 - Chunky Challenges/Passwords.py", line 150, in main New_ID = create_ID(tmp) # gets New_ID from create_ID File "c:/Users/djwil/Documents/python/learning python/Chapter 19 - Chunky Challenges/Passwords.py", line 18, in create_ID if New_ID in tmp[x][0]: # checks if ID is already in list row by row TypeError: list indices must be integers or slices, not list
But I think that I'm defining it as an integer of the a specific place in the list, can someone offer advice?
Reply
#2
After for x in tmp it should probably be if New_Id in x[0]
Reply
#3
If tmp is a list of lists. for x in tmp makes x a list. You cannot use a list as an index into a list. I think you want to do this:
for x in tmp:
    if new_id == x[0]:
tmp is a bad variable name. It tells me nothing about what the variable is or how I should use it. I think that you would have fewer problems if you started using better variable names.

You should also think about what type of data structures best fit your problem. I don't know anything this challenge, but if you used a dictionary instead of a list of lists your test for unique password is:
if passwords.get(new_id):
    print("That ID is already on the list")
else:
    break;
And one last quibble. Printing is not the same as returning.
print("That ID is already on the list") # if it is return this error message 
Misleading comments are far worse than no comment at all.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to efficiently average same entries of lists in a list xquad 5 323 Dec-17-2021, 04:44 PM
Last Post: xquad
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 2 378 Nov-13-2021, 07:47 PM
Last Post: AlphaInc
  sorting a list of lists by an element leapcfm 3 516 Sep-10-2021, 03:33 PM
Last Post: leapcfm
  behavior list of lists roym 5 722 Jul-04-2021, 04:43 PM
Last Post: roym
  List of lists - merge sublists with common elements medatib531 1 1,410 May-09-2021, 07:49 AM
Last Post: Gribouillis
  Sort List of Lists by Column Nju 1 956 Apr-13-2021, 11:59 PM
Last Post: bowlofred
  Creating list of lists from generator object t4keheart 1 1,041 Nov-13-2020, 04:59 AM
Last Post: perfringo
  How do i make a new lists out of an list ozezn1 1 743 Oct-28-2020, 10:19 PM
Last Post: Gribouillis
  TypeError: string indices must be integers hendern 2 1,414 Oct-02-2020, 10:16 PM
Last Post: hendern
  List index out of range error while accessing 2 lists in python K11 2 949 Sep-29-2020, 05:24 AM
Last Post: K11

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020